Merge lp:~kiril-vladimiroff/cloud-init/cloudsigma-base64-userdata into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Kiril Vladimiroff
Status: Merged
Merged at revision: 965
Proposed branch: lp:~kiril-vladimiroff/cloud-init/cloudsigma-base64-userdata
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 80 lines (+22/-2)
3 files modified
cloudinit/sources/DataSourceCloudSigma.py (+5/-0)
doc/sources/cloudsigma/README.rst (+4/-0)
tests/unittests/test_datasource/test_cloudsigma.py (+13/-2)
To merge this branch: bzr merge lp:~kiril-vladimiroff/cloud-init/cloudsigma-base64-userdata
Reviewer Review Type Date Requested Status
cloud-init Commiters Pending
Review via email: mp+207114@code.launchpad.net

Description of the change

Read encoded with base64 user data in the CloudSigma datasource.

This allows users of CloudSigma's VM to encode their user data with base64.
In order to do that they have to add the ``cloudinit-user-data`` field to
the ``base64_fields``. The latter is a comma-separated field with
all the meta fields whit base64 encoded values.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cloudinit/sources/DataSourceCloudSigma.py'
2--- cloudinit/sources/DataSourceCloudSigma.py 2014-02-18 16:58:12 +0000
3+++ cloudinit/sources/DataSourceCloudSigma.py 2014-02-19 08:53:16 +0000
4@@ -15,6 +15,7 @@
5 #
6 # You should have received a copy of the GNU General Public License
7 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8+from base64 import b64decode
9 import re
10
11 from cloudinit import log as logging
12@@ -61,7 +62,11 @@
13 if dsmode == "disabled" or dsmode != self.dsmode:
14 return False
15
16+ base64_fields = server_meta.get('base64_fields', '').split(',')
17 self.userdata_raw = server_meta.get('cloudinit-user-data', "")
18+ if 'cloudinit-user-data' in base64_fields:
19+ self.userdata_raw = b64decode(self.userdata_raw)
20+
21 self.metadata = server_context
22 self.ssh_public_key = server_meta['ssh_public_key']
23
24
25=== modified file 'doc/sources/cloudsigma/README.rst'
26--- doc/sources/cloudsigma/README.rst 2014-02-13 15:39:39 +0000
27+++ doc/sources/cloudsigma/README.rst 2014-02-19 08:53:16 +0000
28@@ -23,6 +23,10 @@
29 header could be omitted. However since this is a raw-text field you could provide any of the valid
30 `config formats`_.
31
32+You have the option to encode your user-data using Base64. In order to do that you have to add the
33+``cloudinit-user-data`` field to the ``base64_fields``. The latter is a comma-separated field with
34+all the meta fields whit base64 encoded values.
35+
36 If your user-data does not need an internet connection you can create a
37 `meta field`_ in the `server context`_ ``cloudinit-dsmode`` and set "local" as value.
38 If this field does not exist the default value is "net".
39
40=== modified file 'tests/unittests/test_datasource/test_cloudsigma.py'
41--- tests/unittests/test_datasource/test_cloudsigma.py 2014-02-12 10:14:49 +0000
42+++ tests/unittests/test_datasource/test_cloudsigma.py 2014-02-19 08:53:16 +0000
43@@ -1,4 +1,5 @@
44 # coding: utf-8
45+import copy
46 from unittest import TestCase
47
48 from cloudinit.cs_utils import Cepko
49@@ -24,7 +25,8 @@
50
51
52 class CepkoMock(Cepko):
53- result = SERVER_CONTEXT
54+ def __init__(self, mocked_context):
55+ self.result = mocked_context
56
57 def all(self):
58 return self
59@@ -33,7 +35,7 @@
60 class DataSourceCloudSigmaTest(TestCase):
61 def setUp(self):
62 self.datasource = DataSourceCloudSigma.DataSourceCloudSigma("", "", "")
63- self.datasource.cepko = CepkoMock()
64+ self.datasource.cepko = CepkoMock(SERVER_CONTEXT)
65 self.datasource.get_data()
66
67 def test_get_hostname(self):
68@@ -57,3 +59,12 @@
69 def test_user_data(self):
70 self.assertEqual(self.datasource.userdata_raw,
71 SERVER_CONTEXT['meta']['cloudinit-user-data'])
72+
73+ def test_encoded_user_data(self):
74+ encoded_context = copy.deepcopy(SERVER_CONTEXT)
75+ encoded_context['meta']['base64_fields'] = 'cloudinit-user-data'
76+ encoded_context['meta']['cloudinit-user-data'] = 'aGkgd29ybGQK'
77+ self.datasource.cepko = CepkoMock(encoded_context)
78+ self.datasource.get_data()
79+
80+ self.assertEqual(self.datasource.userdata_raw, b'hi world\n')