aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-installer/anaconda/files/0040-support-downloading-file-from-http-ftp-server-to-tar.patch
blob: 37ca585120dd66f47ec91866909efc792726c575 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
From 4c891449e324d3a78ddf7722fd7805bae2e1cbfb 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>

Rebase for anaconda 34.

Signed-off-by: Kai Kang <kai.kang@windriver.com>

Rebase for anaconda 37.

Signed-off-by: Kai Kang <kai.kang@windriver.com>

Rebase for anaconda 38 on 20231107.

Signed-off-by: Kai Kang <kai.kang@windriver.com>
---
 pyanaconda/installation.py |   6 +++
 pyanaconda/kickstart.py    | 100 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+)

diff --git a/pyanaconda/installation.py b/pyanaconda/installation.py
index 9c1372f606..803687d11d 100644
--- a/pyanaconda/installation.py
+++ b/pyanaconda/installation.py
@@ -184,6 +184,12 @@ class RunInstallationTask(InstallationTask):
             _("Configuring addons")
         )
 
+        addon_config.append(Task(
+            "Configure download",
+            ksdata.download.execute,
+            (ksdata,)
+        ))
+
         boss_proxy = BOSS.get_proxy()
         for service_name, object_path in boss_proxy.CollectInstallSystemTasks():
             task_proxy = DBus.get_proxy(service_name, object_path)
diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
index 9f235c1d58..df31b8e44c 100644
--- a/pyanaconda/kickstart.py
+++ b/pyanaconda/kickstart.py
@@ -25,6 +25,7 @@ import sys
 import tempfile
 import time
 import warnings
+import requests
 
 from contextlib import contextmanager
 
@@ -45,6 +46,8 @@ from pykickstart.constants import KS_SCRIPT_POST, KS_SCRIPT_PRE, KS_SCRIPT_TRACE
 from pykickstart.errors import KickstartError, KickstartParseWarning, KickstartAuthError
 from pykickstart.ko import KickstartObject
 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, PostScriptSection, PreScriptSection, \
     PreInstallScriptSection, OnErrorScriptSection, TracebackScriptSection, Section
@@ -181,6 +184,101 @@ class UselessObject(KickstartObject):
         """Generate this part of a kickstart file from the DBus module."""
         return ""
 
+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 = conf.target.system_root + 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
 ###
@@ -208,11 +306,13 @@ class AnacondaKickstartSpecification(KickstartSpecification):
         "text": COMMANDS.DisplayMode,
         "updates": COMMANDS.Updates,
         "vnc": COMMANDS.Vnc,
+        "download": Download,
     }
 
     commands_data = {
         "DriverDiskData": COMMANDS.DriverDiskData,
         "SshPwData": COMMANDS.SshPwData,
+        "DownloadData": DownloadData,
     }
 
     @classmethod
-- 
2.7.4