-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_repos.rb
118 lines (106 loc) · 3.1 KB
/
setup_repos.rb
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
#!/usr/bin/env ruby
require 'gitlab'
def client
@client ||= Gitlab.client
end
def devops_group
unless @devops_group
begin
@devops_group = client.create_group('devops', 'devops')
rescue Gitlab::Error::BadRequest => e
if e.response_status == 400
@devops_group = client.group('devops')
end
end
end
@devops_group
end
def create_puppet_file(proj)
begin
client.create_file(proj.id, 'Puppetfile', 'master', puppetfile_content, 'init commit')
rescue Gitlab::Error::BadRequest => e
if e.response_status == 400
# already created
end
end
end
def create_branch(proj_id, branch, ref)
begin
client.create_branch(proj_id, branch, ref)
rescue Gitlab::Error::BadRequest => e
if e.response_status == 400
puts "Branch already created"
else
raise e
end
end
end
def create_control_repo
begin
proj = client.create_project('control-repo', namespace_id: devops_group.id)
create_puppet_file(proj)
create_branch(proj.id, 'dev', 'master')
create_branch(proj.id, 'qa', 'master')
create_branch(proj.id, 'integration', 'master')
create_branch(proj.id, 'acceptance', 'master')
create_branch(proj.id, 'production', 'master')
client.unprotect_branch(proj.id, 'master')
rescue Gitlab::Error::BadRequest => e
if e.response_status == 400
# already created
proj = client.project("devops/control-repo")
create_branch(proj.id, 'dev', 'master')
create_branch(proj.id, 'qa', 'master')
create_branch(proj.id, 'integration', 'master')
create_branch(proj.id, 'acceptance', 'master')
create_branch(proj.id, 'production', 'master')
client.unprotect_branch(proj.id, 'master')
# client.delete_branch(proj.id, 'master')
create_puppet_file(proj)
end
end
end
def modules
<<-EOF
# Example42 v4.x modules (Used in various profiles)
mod 'docker',
:git => 'https://github.com/example42/puppet-docker'
mod 'network',
:git => 'https://github.com/example42/puppet-network'
mod 'apache',
:git => 'https://github.com/example42/puppet-apache',
:branch => '4.x'
mod 'puppet',
:git => 'https://github.com/example42/puppet-puppet',
:branch => 'master'
mod 'rails',
:git => 'https://github.com/example42/puppet-rails'
mod 'ansible',
:git => 'https://github.com/example42/puppet-ansible'
mod 'icinga',
:git => 'https://github.com/example42/puppet-icinga',
:branch => '4.x'
EOF
end
def puppetfile_content
@puppetfile_content ||= ''
end
def mod(name, *args)
url = args.first[:git]
begin
proj = client.create_project(name, import_url: url, namespace_id: devops_group.id)
rescue Gitlab::Error::BadRequest => e
if e.response_status == 400
proj = client.project("devops/#{name}")
end
end
args.first[:git] = proj.ssh_url_to_repo
data = args.first.sort.map { |k, v| ":#{k} => '#{v}'" }.join(",\n ")
puppetfile_content << "mod '#{name}',\n #{data}\n\n"
end
create_control_repo
eval(modules)
#
# client.create_user('[email protected]', 'password', 'joe', { name: 'Joe Smith' })
# add the ssh key
# create_ssh_key(title, key)