~ubuntu-on-ec2/vmbuilder/jenkins_kvm-add-azure-cc

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
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    Copyright 2013, Ben Howard <ben.howard@ubuntu.com>
#
import argparse
import base64
import json
import shutil
import os
import os.path
import sys
import xml.etree.ElementTree as ET
import xml.dom.minidom
from OpenSSL.crypto import *

parser = argparse.ArgumentParser()
parser.add_argument('--file',
        metavar='file',
        type=str,
        required=True,
        help='Your .publishsettings file')
parser.add_argument('--out',
        help="Where to dump the stuff",
        default=None,
        required=True)
parser.add_argument('--base64',
        action="store_true",
        default=False,
        help="Read the file in as base64")

args = parser.parse_args()

tree = None

# Allow for reading the settings as base64
if args.base64:
    ET.fromstring
    decoded = None
    with open(args.file) as f:
        decoded = base64.b64decode(f.read())
    f.close()
    tree = xml.etree.ElementTree.fromstring(decoded)
else:
    tree = xml.etree.ElementTree.parse(args.file)

assert tree is not None

pp = tree.find('PublishProfile')
raw_cert, management_host, cert, subscription_id = None, None, None, None

if pp.get('SchemaVersion') == "2.0":
    subscription = pp.find('Subscription')
    management_host = subscription.get('ServiceManagementUrl')
    raw_cert = subscription.get('ManagementCertificate')
    subscription_id = pp.find('Subscription').get('Id')

else:
    management_host = pp.get('Url')
    raw_cert = pp.get('ManagementCertificate')
    subscription_id = pp.find('Subscription').get('Id')

cert = load_pkcs12(base64.decodestring(raw_cert))
config_json = { 'endpoint': management_host,
                'subscription': subscription_id }

management_cert = "".join([
                    dump_privatekey(FILETYPE_PEM, cert.get_privatekey()),
                    dump_certificate(FILETYPE_PEM, cert.get_certificate()),
                    ])

# The cert is RSA, but for some reason it wasn't encoded properly. GAK
management_cert = management_cert.replace('BEGIN PRIVATE', 'BEGIN RSA PRIVATE')
management_cert = management_cert.replace('END PRIVATE', 'END RSA PRIVATE')

# Make sure the spot exists
azure_out = str(os.path.abspath("%s/.azure" % args.out))
if os.path.exists(azure_out) and \
    not os.path.isdir(azure_out):
    raise Exception("%s must not be a regular file" % azure_out)

elif not os.path.exists(azure_out):
    print "Putting the files in %s" % azure_out
    os.makedirs(azure_out)

# Write the cert path
cert_path = "%s/managementCertificate.pem" % azure_out
with open(cert_path, 'w') as f:
    f.write(management_cert)
f.close()

# Write the config_json
json_path = "%s/config.json" % azure_out
with open(json_path, 'w') as f:
    json.dump(config_json, f)
f.close()

# Copy the settings into place
settings_path = "%s/publishSettings.xml" % azure_out
if not os.path.exists(settings_path):
    shutil.copy(args.file, settings_path)