-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit-manager.py
241 lines (202 loc) · 8.06 KB
/
git-manager.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import yaml
import sys
from os import path
import tempfile
from subprocess import check_output
from os import listdir
from os.path import isfile, join
from shutil import copyfile
import json
import logging
global LOGGER
MANDATORY_GENERAL_PARAMETERS = [
'pr-name',
'branch-name',
'commit-message',
'git-add',
'playbook-dir'
]
def create_logger():
# create logger with 'spam_application'
logger = logging.getLogger('git-manager')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('git-manager.logs')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
logger.info("------------------ STARTING ------------------")
logger.info("Logger initialized")
return logger
def error_message(message):
LOGGER.error(message)
LOGGER.info("------------------ ENDING ------------------")
# print("ERROR: {}".format(message))
exit(1)
def validate_config(config):
LOGGER.debug("Starting to validate config")
if 'orgs' not in config:
error_message("'orgs' was not provided in the config file")
if 'general' not in config:
error_message("'general' was not provided in the config file")
general = config['general']
for mandatory_parameter in MANDATORY_GENERAL_PARAMETERS:
if mandatory_parameter not in general:
error_message("'{}' was not provided in the config file".format(mandatory_parameter))
LOGGER.debug("Successfully validated config")
def get_repo_dir(tmp_folder, org, repo):
return "{}/{}/{}".format(tmp_folder, org, repo)
def get_config_file_path():
LOGGER.debug("Starting to get config file path")
# default config file path
config_file_path = "git-manager-config.yml"
# it can be provided as an argument
if len(sys.argv) == 2:
config_file_path = sys.argv[1]
# validate the config file path actuall exists
if not path.exists(config_file_path):
error_message("'{}' does not exists or is not readable".format(config_file_path))
LOGGER.debug("Using config file: '{}'".format(config_file_path))
LOGGER.debug("Successfully retrieved config file")
return config_file_path
def subprocess_command(command, working_dir):
LOGGER.info("executing: {}".format(" ".join(command)))
LOGGER.info(check_output(command, cwd=working_dir).decode('utf-8'))
def git_command(working_dir, command, org, repo):
git_url = "https://github.com/{}/{}.git".format(org, repo)
repo_dir = get_repo_dir(working_dir, org, repo)
command = ['git', command, git_url, repo_dir]
subprocess_command(command, None)
def git_clone(working_dir, org, repo):
git_command(working_dir, "clone", org, repo)
def git_branch(working_dir, org, repo, branch_name):
command = ['git', 'branch', branch_name]
repo_dir = get_repo_dir(working_dir, org, repo)
subprocess_command(command, repo_dir)
def git_checkout(working_dir, org, repo, branch_name):
command = ['git', 'checkout', branch_name]
repo_dir = get_repo_dir(working_dir, org, repo)
subprocess_command(command, repo_dir)
def run_playbook(working_dir, org, repo, extra_vars):
command = ['ansible-playbook', 'run.yml']
if extra_vars is not None:
command.append('--extra-vars')
command.append(json.dumps(extra_vars))
repo_dir = get_repo_dir(working_dir, org, repo)
subprocess_command(command, repo_dir)
def get_config():
config_file_path = get_config_file_path()
# read the config content
config_content = open(config_file_path, 'r').read()
# create the python dictionary of the config, this will throw yaml
# exception if the config does not meet yaml standards
config = yaml.load(config_content)
# validate mandatory config fields
validate_config(config)
config_pprint = json.dumps(config, sort_keys=True, indent=4, separators=(',', ': '))
LOGGER.info("git manager configuration: " + config_pprint)
return config
def create_working_dir():
dir_path = tempfile.mkdtemp()
LOGGER.info("Created a tmp directory for cloning repos: " + dir_path)
return dir_path
def clone_and_setup_repos(config, working_dir):
for org, value in config['orgs'].items():
repos = value['repos']
for repo in repos:
LOGGER.info("Cloning repo '{}/{}' and copying over files located in playbook directory".format(org, repo))
git_clone(working_dir, org, repo)
branch_name = config['general']['branch-name']
# if we are creating branch then lets do it
if config['general'].get('create-branch') is True:
git_branch(working_dir, org, repo, branch_name)
# now checkout branch
git_checkout(working_dir, org, repo, branch_name)
playbook_dir = config['general']['playbook-dir']
# now copy over the playbook
playbook_files = [f for f in listdir(playbook_dir) if isfile(join(playbook_dir, f))]
for playbook_file in playbook_files:
repo_dir = get_repo_dir(working_dir, org, repo)
copyfile(playbook_dir + "/" + playbook_file, repo_dir + "/" + playbook_file)
LOGGER.info("Finished cloning repo '{}/{}'".format(org, repo))
def update_repos(config, working_dir):
extra_vars = {}
if 'extra-vars' in config['general']:
extra_vars = config['general']['extra-vars']
for org, value in config['orgs'].items():
repos = value['repos']
for repo in repos:
LOGGER.info("Running 'run.yml' in '{}/{}'".format(org, repo))
extra_vars['org'] = org
extra_vars['repo'] = repo
run_playbook(working_dir, org, repo, extra_vars)
LOGGER.info("Finished running 'run.yml' in '{}/{}'".format(org, repo))
def git_add(config, working_dir, org, repo):
# this should be a list
add_files = config['general']['git-add']
command = ['git', 'add']
command.extend(add_files)
repo_dir = get_repo_dir(working_dir, org, repo)
subprocess_command(command, repo_dir)
def git_commit(config, working_dir, org, repo):
commit_message = config['general']['commit-message']
command = ['git', 'commit', '-m', commit_message]
repo_dir = get_repo_dir(working_dir, org, repo)
subprocess_command(command, repo_dir)
def git_push(config, working_dir, org, repo):
branch_name = config['general']['branch-name']
command = ['git', 'push', '--set-upstream', 'origin', branch_name]
repo_dir = get_repo_dir(working_dir, org, repo)
subprocess_command(command, repo_dir)
def git_add_commit_push_repos(config, working_dir):
for org, value in config['orgs'].items():
repos = value['repos']
for repo in repos:
git_add(config, working_dir, org, repo)
git_commit(config, working_dir, org, repo)
git_push(config, working_dir, org, repo)
def git_delete_branch_locally(config, working_dir, org, repo):
branch_name = config['general']['branch-name']
command = ['git', 'branch', '-d', branch_name]
repo_dir = get_repo_dir(working_dir, org, repo)
subprocess_command(command, repo_dir)
def git_delete_branch_remote(config, working_dir, org, repo):
branch_name = config['general']['branch-name']
command = ['git', 'push', 'origin', '--delete', branch_name]
repo_dir = get_repo_dir(working_dir, org, repo)
subprocess_command(command, repo_dir)
def git_delete_branches(config, working_dir):
for org, value in config['orgs'].items():
repos = value['repos']
for repo in repos:
git_checkout(working_dir, org, repo, "master")
git_delete_branch_locally(config, working_dir, org, repo)
git_delete_branch_remote(config, working_dir, org, repo)
def run_git_manager(config, working_dir):
if config['general'].get('delete-branch') is True:
LOGGER.info("Starting to delete branches from repos")
git_delete_branches(config, working_dir)
return
# if note deleting branch then
# assume adding a branch
LOGGER.info("Starting to create branches in repos")
update_repos(config, working_dir)
git_add_commit_push_repos(config, working_dir)
def main():
config=get_config()
working_dir=create_working_dir()
clone_and_setup_repos(config, working_dir)
run_git_manager(config, working_dir)
if __name__ == "__main__":
LOGGER = create_logger()
main()
LOGGER.info("------------------ ENDING ------------------")
# shutil.rmtree(dir_path)