This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PHD3-RepoPrepare.py
163 lines (127 loc) · 4.55 KB
/
PHD3-RepoPrepare.py
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
__author__ = 'dbaskette'
# Download Software
# Setup Repos
# Make changes for Repo Distribution
# Install Ambari-Server
import json
import argparse
import tarfile
import os
import boto
import boto.s3.connection
from boto.s3.connection import Location
from boto.s3.key import Key
from simplethreads.ThreadPool import ThreadPool
import yum
baseDir = "/mnt/phd3/software/"
ambariBucket = "ambari-repo"
def getBucketName(fileType):
with open("./phd3.json", "r") as phd3Files:
fileNames = json.load(phd3Files)
for file in fileNames:
if file == fileType:
return fileNames[file]
return 0
def downloadSoftware(key, fileName):
try:
key.get_contents_to_filename(baseDir + fileName)
except Exception as e:
pass
def getSoftware(awsKey, secretKey):
bucketName = getBucketName("bucket")
fileNames = []
conn = boto.connect_s3(aws_access_key_id=awsKey, aws_secret_access_key=secretKey)
pool = ThreadPool(10)
if not os.path.exists(baseDir):
os.makedirs(baseDir)
if conn.get_bucket(bucketName) is None:
print "BUCKET DOES NOT EXIST!"
exit(0)
else:
bucket = conn.get_bucket(bucketName)
for key in bucket.get_all_keys():
fileName = str(key).split(",")[1][:-1]
print fileName
pool.process(downloadSoftware, key, fileName)
if "tar" in fileName:
fileNames.append(fileName)
pool.shutdown()
conn.close()
print fileNames
return fileNames
def createRepo(fileNames, logFile):
yb = yum.YumBase()
inst = yb.rpmdb.returnPackages()
installed = [x.name for x in inst]
packages = ['httpd']
for package in packages:
if package in installed:
print('{0} is already installed'.format(package))
else:
print('Installing {0}'.format(package))
kwarg = {
'name': package
}
yb.install(**kwarg)
yb.resolveDeps()
yb.buildTransaction()
yb.processTransaction()
os.system("service httpd restart")
for fileName in fileNames:
try:
tar = tarfile.open(baseDir + fileName, "r:gz")
tarMembers = tar.getmembers()
tar.extractall(baseDir)
tar.close()
repoName = (str(tarMembers[0]).split(" ")[1]).strip('\'')
repoPath = baseDir + repoName
os.system(repoPath + "/setup_repo.sh")
except Exception as e:
pass
def allowSSH():
lines = []
with open("/etc/ssh/sshd_config", "r")as origFile:
contents = origFile.read()
contents = contents.replace("PasswordAuthentication no", "PasswordAuthentication yes \n")
# contents = contents.replace("#PubkeyAuthentication", "PubkeyAuthentication")
with (open("/etc/ssh/sshd_config", "w")) as newFile:
newFile.write(contents)
os.system("service sshd restart")
def uploadRepo(awsKey, secretKey, stack, logFile):
bucketName = stack + "-" + ambariBucket
try:
conn = boto.connect_s3(aws_access_key_id=awsKey, aws_secret_access_key=secretKey)
conn.create_bucket(bucketName, location=Location.DEFAULT)
bucket = conn.get_bucket(bucketName)
k = Key(bucket)
k.key = 'ambari.repo'
k.set_contents_from_filename("/etc/yum.repos.d/ambari.repo")
k.set_canned_acl('public-read')
except Exception as e:
logFile.write(e)
#
# def documentRepos(repoName):
# print "Document Repo"
# with open("repo-info.txt", "w+") as repoList:
# repoList.write(repoName)
#
#
def cliParse():
VALID_ACTION = ["get", "set"]
parser = argparse.ArgumentParser(description='Amazon S3 Download')
subparsers = parser.add_subparsers(help='sub-command help', dest="subparser_name")
parser_get = subparsers.add_parser("get", help="Get a file from S3")
parser_get.add_argument("--key", dest='accessKey', action="store", help="Your access key", required=False)
parser_get.add_argument("--secret", dest='secretKey', action="store", help="Your Secret key", required=False)
parser_get.add_argument("--stack", dest='stack', action="store", help="StackName", required=False)
args = parser.parse_args()
return args
def prepareEnv(args):
logFile = open("repo-prepare.log", "w+")
allowSSH()
fileNames = getSoftware(args.accessKey, args.secretKey)
createRepo(fileNames, logFile)
uploadRepo(args.accessKey, args.secretKey, args.stack, logFile)
if __name__ == '__main__':
print "PHD3 Installer"
prepareEnv(cliParse())