danarchy_deploy 0.1.4 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/Gemfile.lock +9 -20
  4. data/README.md +10 -9
  5. data/Rakefile +0 -4
  6. data/bin/danarchy_deploy +24 -10
  7. data/danarchy_deploy.gemspec +4 -3
  8. data/lib/danarchy_deploy.rb +186 -55
  9. data/lib/danarchy_deploy/applicator.rb +39 -0
  10. data/lib/danarchy_deploy/applicator/nginx.rb +86 -0
  11. data/lib/danarchy_deploy/applicator/phpfpm.rb +84 -0
  12. data/lib/danarchy_deploy/applicator/redmine.rb +40 -0
  13. data/lib/danarchy_deploy/applicator/ssl.rb +14 -0
  14. data/lib/danarchy_deploy/applicator/wordpress.rb +146 -0
  15. data/lib/danarchy_deploy/applicator/wordpress/wpcli.rb +67 -0
  16. data/lib/danarchy_deploy/applicator/wordpress/wpcli_install.sh +36 -0
  17. data/lib/danarchy_deploy/applicator/wordpress/wpconfig.rb +49 -0
  18. data/lib/danarchy_deploy/archiver.rb +9 -7
  19. data/lib/danarchy_deploy/archiver/svn.rb +17 -0
  20. data/lib/danarchy_deploy/hash_deep_merge.rb +9 -0
  21. data/lib/danarchy_deploy/helpers.rb +42 -10
  22. data/lib/danarchy_deploy/services.rb +12 -25
  23. data/lib/danarchy_deploy/services/init.rb +52 -0
  24. data/lib/danarchy_deploy/services/init/openrc.rb +72 -0
  25. data/lib/danarchy_deploy/services/init/systemd.rb +69 -0
  26. data/lib/danarchy_deploy/services/mongodb.rb +162 -0
  27. data/lib/danarchy_deploy/services/mysql.rb +58 -0
  28. data/lib/danarchy_deploy/services/mysql/new_server.rb +72 -0
  29. data/lib/danarchy_deploy/services/mysql/privileges.rb +35 -0
  30. data/lib/danarchy_deploy/system.rb +86 -0
  31. data/lib/danarchy_deploy/system/centos.rb +17 -0
  32. data/lib/danarchy_deploy/system/debian.rb +61 -0
  33. data/lib/danarchy_deploy/system/gentoo.rb +66 -0
  34. data/lib/danarchy_deploy/system/opensuse.rb +22 -0
  35. data/lib/danarchy_deploy/templater.rb +53 -13
  36. data/lib/danarchy_deploy/users.rb +28 -18
  37. data/lib/danarchy_deploy/version.rb +1 -1
  38. data/templates/deploy_template.json +3 -3
  39. metadata +34 -12
@@ -0,0 +1,66 @@
1
+
2
+ module DanarchyDeploy
3
+ module System
4
+ class Gentoo
5
+ def self.new(deployment, options)
6
+ puts "\n" + self.name
7
+ puts 'Gentoo detected! Using emerge.'
8
+
9
+ hostname = deployment[:hostname]
10
+ if check_hostname(hostname) == false
11
+ puts "Setting hostname to: #{hostname}"
12
+ set_hostname(hostname)
13
+ end
14
+
15
+ installer = 'emerge --usepkg --buildpkg --quiet --noreplace '
16
+ # This needs cpuid2cpuflags to build make.conf; don't --pretend here.
17
+ system("qlist -I cpuid2cpuflags &>/dev/null || #{installer} cpuid2cpuflags &>/dev/null")
18
+ installer += '--pretend ' if options[:pretend]
19
+
20
+ updater = 'emerge --usepkg --buildpkg --update --deep --newuse --quiet --with-bdeps=y @world'
21
+ updater += ' --pretend' if options[:pretend]
22
+
23
+ cleaner = 'emerge --depclean --quiet '
24
+ cleaner += '--pretend ' if options[:pretend]
25
+
26
+ if deployment[:portage]
27
+ if deployment[:portage][:templates]
28
+ puts "\nChecking Portage configs."
29
+ DanarchyDeploy::Templater.new(deployment[:portage][:templates], options)
30
+ end
31
+
32
+ emerge_sync(options) if deployment[:portage][:sync]
33
+ end
34
+
35
+ [installer, updater, cleaner]
36
+ end
37
+
38
+ private
39
+
40
+ def self.emerge_sync(options)
41
+ File.open('/tmp/datetime', 'a+') do |f|
42
+ last_sync = f.getbyte ? DateTime.parse(f.read) : (DateTime.now - 2)
43
+
44
+ if (DateTime.now - last_sync).to_i != 0
45
+ puts "\nUpdating Portage repo..."
46
+ DanarchyDeploy::Helpers.run_command('emerge --sync --quiet 2>/dev/null', options)
47
+ f.truncate(0)
48
+ f.write DateTime.now
49
+ end
50
+
51
+ f.close
52
+ end
53
+ end
54
+
55
+ def self.set_hostname(hostname)
56
+ confd_hostname = "hostname=\"#{hostname}\""
57
+ File.write('/etc/conf.d/hostname', confd_hostname)
58
+ `hostname #{hostname}`
59
+ end
60
+
61
+ def self.check_hostname(hostname)
62
+ `hostname`.chomp == hostname
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,22 @@
1
+
2
+ module DanarchyDeploy
3
+ module System
4
+ class OpenSUSE
5
+ def self.new(deployment, options)
6
+ puts "\n" + self.name
7
+ puts "#{deployment[:os].capitalize} detected! Using zypper."
8
+
9
+ puts "Updating zypper repositories..."
10
+ DanarchyDeploy::Helpers.run_command('sudo zypper refresh', options)
11
+
12
+ installer = 'zypper install '
13
+ updater = 'zypper upgrade'
14
+ cleaner = nil
15
+ zypper_refresh_repos = DanarchyDeploy::Helpers.run_command('zypper refresh', options)
16
+ # Needs package checking & testing
17
+
18
+ [installer, updater, cleaner]
19
+ end
20
+ end
21
+ end
22
+ end
@@ -6,10 +6,17 @@ module DanarchyDeploy
6
6
  def self.new(templates, options)
7
7
  puts "\n" + self.name
8
8
 
9
+ if templates.map(&:keys).flatten.include?(:remove)
10
+ puts ' > Removing templates tagged for removal.'
11
+ templates = remove_templates(templates, options)
12
+ end
13
+
9
14
  templates.each do |template|
15
+ next if template[:remove]
10
16
  abort("No target destination set for template: #{template[:source]}!") if !template[:target]
11
17
  abort("No source or data for template: #{template[:target]}") if !template[:source] && !template[:data]
12
-
18
+ abort("Source file does not exist at: #{template[:source]}") if ! File.exist?(template[:source])
19
+
13
20
  target = template[:target]
14
21
  source = template[:source] || '-- encoded data --'
15
22
  dir_perms = template[:dir_perms]
@@ -19,12 +26,13 @@ module DanarchyDeploy
19
26
  puts " Source: #{source}"
20
27
  puts " |> Dir Permissions: #{dir_perms || '--undefined--'}"
21
28
  puts " |> File Permissions: #{file_perms || '--undefined--'}"
22
- puts " |> Variables: #{@variables}" if @variables
29
+ if options[:vars_verbose] && @variables
30
+ puts ' |> Variables: '
31
+ var = DanarchyDeploy::Helpers.hash_except(@variables, '^pass')
32
+ puts var
33
+ end
23
34
 
24
- targetdir = File.dirname(target)
25
35
  tmpdir = options[:deploy_dir] + '/' + File.basename(File.dirname(target))
26
- p tmpdir
27
- Dir.exist?(targetdir) || FileUtils.mkdir_p(targetdir, mode: 0755)
28
36
  Dir.exist?(tmpdir) || FileUtils.mkdir_p(tmpdir, mode: 0755)
29
37
  tmpfile = tmpdir + '/' + File.basename(target) + '.tmp'
30
38
 
@@ -35,25 +43,25 @@ module DanarchyDeploy
35
43
  end
36
44
 
37
45
  File.open(tmpfile, 'w') do |f|
38
- result = ERB.new(File.read(source)).result(binding)
46
+ result = ERB.new(File.read(source), nil, '-').result(binding)
39
47
  f.write(result)
40
48
  f.close
41
49
  end
42
50
 
43
51
  result = { write_erb: [], verify_permissions: {} }
44
52
  if options[:pretend]
45
- diff(target, tmpfile)
46
- puts "\n - Fake Run: Not changing '#{target}'."
53
+ diff(target, tmpfile) if options[:vars_verbose]
54
+ puts "\n - Fake Run: Not changing '#{target}'."
47
55
  result[:verify_permissions][File.dirname(tmpfile)] = verify_permissions(File.dirname(tmpfile), dir_perms, options)
48
56
  result[:verify_permissions][tmpfile] = verify_permissions(tmpfile, file_perms, options)
49
57
  elsif md5sum(target,tmpfile) == true
50
- puts "\n - No change in '#{target}': Nothing to update here."
58
+ puts "\n - No change in '#{target}': Nothing to update here."
51
59
  result[:verify_permissions][File.dirname(target)] = verify_permissions(File.dirname(target), dir_perms, options)
52
60
  result[:verify_permissions][target] = verify_permissions(target, file_perms, options)
53
61
  else
54
- diff(target, tmpfile)
62
+ diff(target, tmpfile) if options[:vars_verbose]
55
63
  result[:write_erb] = enable_erb(target, tmpfile)
56
- puts " => #{target} was updated!"
64
+ puts " |+ #{target} was updated!"
57
65
  result[:verify_permissions][File.dirname(target)] = verify_permissions(File.dirname(target), dir_perms, options)
58
66
  result[:verify_permissions][target] = verify_permissions(target, file_perms, options)
59
67
  end
@@ -69,6 +77,7 @@ module DanarchyDeploy
69
77
  chmod = nil
70
78
  puts "\n > Verifying ownership and permissions for '#{target}'"
71
79
  if perms
80
+ puts " |+ Setting file mode to: #{perms[:mode]}"
72
81
  (owner, group, mode) = perms[:owner], perms[:group], perms[:mode]
73
82
  else
74
83
  if File.stat(target).mode & 07777 == '0777'.to_i(8)
@@ -142,11 +151,11 @@ module DanarchyDeploy
142
151
 
143
152
  def self.diff(target, tmpfile)
144
153
  if File.exist?(target) && File.exist?(tmpfile)
145
- puts "\n !! Diff between #{target} <=> #{tmpfile}"
154
+ puts "\n!! Diff between #{target} <=> #{tmpfile}"
146
155
  IO.popen("diff -Naur #{target} #{tmpfile}") do |o|
147
156
  puts o.read
148
157
  end
149
- puts "\n !! End Diff \n\n"
158
+ puts "\n!! End Diff \n\n"
150
159
  elsif File.exist?(target) && !File.exist?(tmpfile)
151
160
  return false
152
161
  end
@@ -154,11 +163,42 @@ module DanarchyDeploy
154
163
 
155
164
  def self.enable_erb(target, tmpfile)
156
165
  puts "\n |+ Moving #{tmpfile} => #{target}"
166
+ targetdir = File.dirname(target)
167
+ Dir.exist?(targetdir) || FileUtils.mkdir_p(targetdir, mode: 0755)
157
168
  system("mv #{tmpfile} #{target}")
158
169
  end
159
170
 
160
171
  def self.write_tmpfile(source, data)
161
172
  File.write(source, data)
162
173
  end
174
+
175
+ def self.remove_templates(templates, options)
176
+ templates.delete_if do |template|
177
+ if template.keys.include?(:remove)
178
+ if options[:pretend]
179
+ puts " - Fake Run - Would have removed: '#{template[:target]}'."
180
+ false
181
+ elsif File.exist?(template[:target])
182
+ puts " |- Removing: #{template[:target]}"
183
+ File.delete(template[:target])
184
+
185
+ dirname = File.dirname(template[:target])
186
+ if Dir.exist?(dirname) && Dir.empty?(dirname)
187
+ puts " |- Removing empty directory: #{dirname}"
188
+ Dir.rmdir(dirname)
189
+ if Dir.exist?(dirname)
190
+ puts " ! Failed to remove directory!"
191
+ else
192
+ puts " - Removed directory."
193
+ end
194
+ end
195
+
196
+ true
197
+ end
198
+ end
199
+ end
200
+
201
+ templates
202
+ end
163
203
  end
164
204
  end
@@ -5,8 +5,9 @@ module DanarchyDeploy
5
5
  puts "\n" + self.name
6
6
  (useradd_result, userdel_result, archives_result) = nil
7
7
 
8
- deployment[:users].each do |user|
9
- puts " > Checking if user '#{user[:username]}' already exists."
8
+ deployment[:users].each do |username, user|
9
+ user[:username] = username.to_s
10
+ puts "\n > Checking if user '#{user[:username]}' already exists."
10
11
  usercheck_result = usercheck(user, options)
11
12
 
12
13
  if usercheck_result[:stdout]
@@ -24,6 +25,7 @@ module DanarchyDeploy
24
25
 
25
26
  puts " |+ Adding user: #{user[:username]}"
26
27
  useradd_result = useradd(user, options)
28
+ File.chmod(0750, user[:home]) if Dir.exist?(user[:home])
27
29
  end
28
30
 
29
31
  if !options[:pretend]
@@ -46,10 +48,12 @@ module DanarchyDeploy
46
48
  end
47
49
  end
48
50
 
49
- if user[:archives] && !user[:archives].empty?
50
- puts " > Deploying archives for #{user[:username]}"
51
- DanarchyDeploy::Archiver.new(user[:archives], options)
51
+ if user[:applications]
52
+ puts "\n > Checking #{user[:username]}'s applications."
53
+ user = DanarchyDeploy::Applicator.new(deployment[:os], user, options)
52
54
  end
55
+
56
+ user.delete(:username)
53
57
  end
54
58
 
55
59
  # [useradd_result, userdel_result]
@@ -60,16 +64,13 @@ module DanarchyDeploy
60
64
  def self.useradd(user, options)
61
65
  useradd_cmd = "useradd #{user[:username]} "
62
66
  useradd_cmd += "--home-dir #{user[:home]} " if user[:home]
67
+ useradd_cmd += "--create-home " if !Dir.exist?(user[:home])
63
68
  useradd_cmd += "--uid #{user[:uid]} " if user[:uid]
64
69
  useradd_cmd += "--gid #{user[:gid]} " if user[:gid]
65
70
  useradd_cmd += "--groups #{user[:groups].join(',')} " if user[:groups]
66
71
  useradd_cmd += "--shell /sbin/nologin " if user[:nologin]
67
72
  useradd_cmd += "--system " if user[:system]
68
- if options[:pretend]
69
- puts "\tFake run: #{useradd_cmd}"
70
- else
71
- DanarchyDeploy::Helpers.run_command(useradd_cmd, options)
72
- end
73
+ DanarchyDeploy::Helpers.run_command(useradd_cmd, options)
73
74
  end
74
75
 
75
76
  def self.userdel(user, options)
@@ -95,11 +96,19 @@ module DanarchyDeploy
95
96
  def self.updategroups(user, options)
96
97
  groups = user[:groups].join(',')
97
98
  groupupdate_cmd = "usermod #{user[:username]} --groups #{groups}"
98
- if options[:pretend]
99
- puts "\tFake run: #{groupupdate_cmd}"
100
- else
101
- DanarchyDeploy::Helpers.run_command(groupupdate_cmd, options)
102
- end
99
+ DanarchyDeploy::Helpers.run_command(groupupdate_cmd, options)
100
+ end
101
+
102
+ def self.add_to_group(user, options)
103
+ groups = user[:groups].join(',')
104
+ groupadd_cmd = "usermod #{user[:username]} --groups #{groups} --append"
105
+ DanarchyDeploy::Helpers.run_command(groupadd_cmd, options)
106
+ end
107
+
108
+ def self.remove_from_group(user, group, options)
109
+ groups = user[:groups].join(',')
110
+ removegroup_cmd = "gpasswd --remove #{user[:username]} #{group}"
111
+ DanarchyDeploy::Helpers.run_command(removegroup_cmd, options)
103
112
  end
104
113
 
105
114
  def self.authorized_keys(user)
@@ -109,12 +118,13 @@ module DanarchyDeploy
109
118
  Dir.exist?(ssh_path) || Dir.mkdir(ssh_path, 0700)
110
119
  File.chown(user[:uid], user[:gid], ssh_path)
111
120
  File.open(authkeys, 'a+') do |f|
121
+ contents = f.read
112
122
  user[:authorized_keys].each do |authkey|
113
- if !f.read.include?(authkey)
123
+ if contents.include?(authkey)
124
+ puts " - Key already in place: #{authkey}"
125
+ else
114
126
  puts " + Adding authorized_key: #{authkey}"
115
127
  f.puts authkey
116
- else
117
- puts ' - No change needed'
118
128
  end
119
129
  end
120
130
 
@@ -1,3 +1,3 @@
1
1
  module DanarchyDeploy
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -37,8 +37,8 @@
37
37
  ],
38
38
  "groups": [
39
39
  {
40
- "groupname": "groupname",
41
- "gid": int,
40
+ "groupname": "groupname",
41
+ "gid": int
42
42
  }
43
43
  ],
44
44
  "services": {
@@ -48,7 +48,7 @@
48
48
  "enable",
49
49
  "restart"
50
50
  ]
51
- }
51
+ },
52
52
  "archives": [
53
53
  {
54
54
  "target": "/path/to/extract/to/",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danarchy_deploy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan James
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-11 00:00:00.000000000 Z
11
+ date: 2020-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danarchy_couchdb
@@ -30,42 +30,42 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.16'
33
+ version: '2.1'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.16'
40
+ version: '2.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec
56
+ name: mongo
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.0'
62
- type: :development
61
+ version: '2.13'
62
+ type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '3.0'
68
+ version: '2.13'
69
69
  description: DanarchyDeploy intends to simplify Gentoo Linux (and other distro) deployments
70
70
  down to a single template from an input JSON or YAML file, or from a CouchDB file.
71
71
  email:
@@ -90,11 +90,34 @@ files:
90
90
  - bin/setup
91
91
  - danarchy_deploy.gemspec
92
92
  - lib/danarchy_deploy.rb
93
+ - lib/danarchy_deploy/applicator.rb
94
+ - lib/danarchy_deploy/applicator/nginx.rb
95
+ - lib/danarchy_deploy/applicator/phpfpm.rb
96
+ - lib/danarchy_deploy/applicator/redmine.rb
97
+ - lib/danarchy_deploy/applicator/ssl.rb
98
+ - lib/danarchy_deploy/applicator/wordpress.rb
99
+ - lib/danarchy_deploy/applicator/wordpress/wpcli.rb
100
+ - lib/danarchy_deploy/applicator/wordpress/wpcli_install.sh
101
+ - lib/danarchy_deploy/applicator/wordpress/wpconfig.rb
93
102
  - lib/danarchy_deploy/archiver.rb
103
+ - lib/danarchy_deploy/archiver/svn.rb
94
104
  - lib/danarchy_deploy/groups.rb
105
+ - lib/danarchy_deploy/hash_deep_merge.rb
95
106
  - lib/danarchy_deploy/helpers.rb
96
107
  - lib/danarchy_deploy/installer.rb
97
108
  - lib/danarchy_deploy/services.rb
109
+ - lib/danarchy_deploy/services/init.rb
110
+ - lib/danarchy_deploy/services/init/openrc.rb
111
+ - lib/danarchy_deploy/services/init/systemd.rb
112
+ - lib/danarchy_deploy/services/mongodb.rb
113
+ - lib/danarchy_deploy/services/mysql.rb
114
+ - lib/danarchy_deploy/services/mysql/new_server.rb
115
+ - lib/danarchy_deploy/services/mysql/privileges.rb
116
+ - lib/danarchy_deploy/system.rb
117
+ - lib/danarchy_deploy/system/centos.rb
118
+ - lib/danarchy_deploy/system/debian.rb
119
+ - lib/danarchy_deploy/system/gentoo.rb
120
+ - lib/danarchy_deploy/system/opensuse.rb
98
121
  - lib/danarchy_deploy/templater.rb
99
122
  - lib/danarchy_deploy/users.rb
100
123
  - lib/danarchy_deploy/version.rb
@@ -119,8 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
142
  - !ruby/object:Gem::Version
120
143
  version: '0'
121
144
  requirements: []
122
- rubyforge_project:
123
- rubygems_version: 2.6.14
145
+ rubygems_version: 3.0.3
124
146
  signing_key:
125
147
  specification_version: 4
126
148
  summary: Pushes deployments locally or remotely based on a JSON/YAML/CouchDB template.