aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-installer/anaconda/files/0040-support-downloading-file-from-http-ftp-server-to-tar.patch
blob: 236a4bbe53ad5f7c7bc24807a766e0a8a6810af0 (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
From 7c7a6e75fe96dfcc53f18a90dcd4e3f5ced7cea8 Mon Sep 17 00:00:00 2001
From: Hongxu Jia <hongxu.jia@windriver.com>
Date: Sat, 29 Jun 2019 15:06:40 +0800
Subject: [PATCH] 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 0423a3d..a7a1d88 100644
--- a/pyanaconda/installation.py
+++ b/pyanaconda/installation.py
@@ -101,6 +101,7 @@ def doConfiguration(storage, payload, ksdata):
     os_config.append(Task("Configure timezone", ksdata.timezone.execute))
     os_config.append(Task("Configure language", ksdata.lang.execute))
     os_config.append(Task("Configure X", ksdata.xconfig.execute))
+    os_config.append(Task("Configure download", ksdata.download.execute, (ksdata,)))
     configuration_queue.append(os_config)
 
     # schedule network configuration (if required)
diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
index 5108a5a..5f8a050 100644
--- a/pyanaconda/kickstart.py
+++ b/pyanaconda/kickstart.py
@@ -62,6 +62,8 @@ from pykickstart.constants import KS_SCRIPT_POST, KS_SCRIPT_PRE, KS_SCRIPT_TRACE
     KS_SCRIPT_PREINSTALL, SELINUX_DISABLED, SELINUX_ENFORCING, SELINUX_PERMISSIVE
 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
@@ -924,6 +926,101 @@ class AnacondaSection(Section):
         """Let %anaconda know no additional data will come."""
         Section.finalize(self)
 
+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=VERSION, description="")
+        op.add_argument("--dest", dest="dest", version=VERSION, required=True, help="")
+        op.add_argument("--src", dest="src", version=VERSION, 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, ksdata):
+        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)
+
+
 ###
 ### HANDLERS
 ###
@@ -965,10 +1062,12 @@ commandMap = {
     "xconfig": XConfig,
     "zerombr": UselessCommand,
     "zfcp": UselessCommand,
+    "download": Download,
 }
 
 dataMap = {
     "RepoData": RepoData,
+    "DownloadData": DownloadData,
 }
 
 superclass = returnClassForVersion(VERSION)
-- 
2.7.4