aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-installer/anaconda/files/0040-support-downloading-file-from-http-ftp-server-to-tar.patch
blob: 7a70fe1c9956e7df9c5630d06fd7bf1043427620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
From aa5ea48ea8a6f1063c39672227f90a6f20b1d318 Mon Sep 17 00:00:00 2001
From: Hongxu Jia <hongxu.jia@windriver.com>
Date: Tue, 24 Jul 2018 17:39:58 +0800
Subject: [PATCH 40/65] support downloading file from http/ftp server to target
 image

Add key in kickstart to support downloading file from http/ftp server to target image,
'download --dest=[file://dir/filename|/dir/filename] --src=[http url| ftp url]'

Here is the example in kickstart file:
---start---
download --dest=/etc/rpm/keys/0x100001 --src=http://128.224.162.159/testkey
download --dest=file://etc/rpm/keys/0x100002 --src=http://128.224.162.159/testkey2
---end---

The file be download to target image (/mnt/image/****). For host image,
we could make use of "%pre" section with invoking shell to do that)

Upstream-Status: Inappropriate [oe specific]

Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
---
 pyanaconda/installation.py |  1 +
 pyanaconda/kickstart.py    | 99 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+)

diff --git a/pyanaconda/installation.py b/pyanaconda/installation.py
index 3672fae..f600595 100644
--- a/pyanaconda/installation.py
+++ b/pyanaconda/installation.py
@@ -94,6 +94,7 @@ def doConfiguration(storage, payload, ksdata, instClass):
     os_config.append(Task("Configure timezone", ksdata.timezone.execute, (storage, ksdata, instClass)))
     os_config.append(Task("Configure language", ksdata.lang.execute, (storage, ksdata, instClass)))
     os_config.append(Task("Configure X", ksdata.xconfig.execute, (storage, ksdata, instClass)))
+    os_config.append(Task("Configure download", ksdata.download.execute, (storage, ksdata, instClass)))
     configuration_queue.append(os_config)
 
     # schedule network configuration (if required)
diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
index 9ffc685..3e6505e 100644
--- a/pyanaconda/kickstart.py
+++ b/pyanaconda/kickstart.py
@@ -82,6 +82,8 @@ from pykickstart.constants import KS_SCRIPT_POST, KS_SCRIPT_PRE, KS_SCRIPT_TRACE
     SNAPSHOT_WHEN_POST_INSTALL, SNAPSHOT_WHEN_PRE_INSTALL
 from pykickstart.errors import KickstartError, KickstartParseError, KickstartAuthError
 from pykickstart.parser import KickstartParser
+from pykickstart.base import KickstartCommand, BaseData
+from pykickstart.options import KSOptionParser
 from pykickstart.parser import Script as KSScript
 from pykickstart.sections import NullSection, PackageSection, PostScriptSection, PreScriptSection, PreInstallScriptSection, \
                                  OnErrorScriptSection, TracebackScriptSection, Section
@@ -2190,6 +2192,101 @@ class Upgrade(commands.upgrade.F20_Upgrade):
         util.ipmi_report(IPMI_ABORTED)
         sys.exit(1)
 
+class DownloadData(BaseData):
+    removedKeywords = BaseData.removedKeywords
+    removedAttrs = BaseData.removedAttrs
+
+    def __init__(self, *args, **kwargs):
+        BaseData.__init__(self, *args, **kwargs)
+        self.dest = kwargs.get("dest", None)
+        self.src = kwargs.get("src", None)
+
+    def __eq__(self, y):
+        return self.dest == y.dest
+
+    def _getArgsAsStr(self):
+        retval = "--dest=%s --src=%s" % (self.dest, self.src)
+        return retval
+
+    def __str__(self):
+        retval = BaseData.__str__(self)
+        retval += "download %s\n" % self._getArgsAsStr()
+        return retval
+
+class Download(KickstartCommand):
+    removedKeywords = KickstartCommand.removedKeywords
+    removedAttrs = KickstartCommand.removedAttrs
+
+    def __init__(self, writePriority=0, *args, **kwargs):
+        KickstartCommand.__init__(self, writePriority, *args, **kwargs)
+        self.op = self._getParser()
+        self.downloadList = kwargs.get("downloadList", [])
+
+    def __str__(self):
+        retval = "# Download file from http/ftp server to target image\n"
+        retval += "# download --dest=[file://dir/filename|/dir/filename] --src=[http url| ftp url]\n"
+        for d in self.downloadList:
+            retval += d.__str__()
+        retval += "\n"
+        return retval
+
+    def _getParser(self):
+        op = KSOptionParser(prog="download", version=F28, description="")
+        op.add_argument("--dest", dest="dest", version=F28, required=True, help="")
+        op.add_argument("--src", dest="src", version=F28, required=True, help="")
+
+        return op
+
+    def parse(self, args):
+        ns = self.op.parse_args(args=args, lineno=self.lineno)
+        dd = self.dataClass()
+        self.set_to_obj(ns, dd)
+        dd.lineno = self.lineno
+
+        # Check for duplicates in the data list.
+        if dd in self.dataList():
+            log.warn(_("A source %s has already been defined.") % dd.src)
+
+        log.info("kickstart downloading %s to %s" % (ns.src, ns.dest))
+        return dd
+
+    def dataList(self):
+        return self.downloadList
+
+    @property
+    def dataClass(self):
+        return self.handler.DownloadData
+
+    def execute(self, storage, ksdata, instClass):
+        if not ksdata.download:
+            return
+
+        for dd in ksdata.download.downloadList:
+            if dd.dest.startswith("file:"):
+                dd.dest = dd.dest[len("file:"):]
+
+            if not dd.dest.startswith("/"):
+                msg = _("The dest %s is not on filesystem" % (dd.dest))
+                stderrLog.critical(msg)
+                sys.exit(1)
+
+            dest = util.getSysroot() + dd.dest
+            log.info("downloading %s to %s" % (dd.src, dest))
+            dest_dir = os.path.dirname(dest)
+            if not os.path.exists(dest_dir):
+                os.makedirs(dest_dir)
+
+            try:
+                request = util.requests_session().get(dd.src)
+            except requests.exceptions.RequestException as e:
+                msg = _("The following error was encountered while downloading %s:\n\n%s" % (dd.src, e))
+                stderrLog.critical(msg)
+                sys.exit(1)
+
+            with open(dest, "wb") as dest_f:
+                dest_f.write(request.content)
+
+
 ###
 ### %anaconda Section
 ###
@@ -2329,6 +2426,7 @@ commandMap = {
     "xconfig": XConfig,
     "zerombr": UselessCommand,
     "zfcp": ZFCP,
+    "download": Download,
 }
 
 dataMap = {
@@ -2340,6 +2438,7 @@ dataMap = {
     "RepoData": RepoData,
     "SnapshotData": SnapshotData,
     "VolGroupData": VolGroupData,
+    "DownloadData": DownloadData,
 }
 
 superclass = returnClassForVersion(version=F28)
-- 
2.7.4