puppet-masterless 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/puppet-masterless +48 -35
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f25c87151bb067f0180ff521e3d9321de1bde8c6
4
- data.tar.gz: 27e83b426523ea8d6424f44826d9ce552ae20aa6
3
+ metadata.gz: 03f3fd21ff83a4bdfef031806d9134d1d8279d04
4
+ data.tar.gz: 366fcad2a93d6c24087b0df3c7c745f6efbf5491
5
5
  SHA512:
6
- metadata.gz: 54f080712152dad70216e838c52664572e945e25fea3f15a4507a8208cece077cd087fbdc6c19a42b7e3c9f7b2db59e180b8bada031bb577dbe0ef8d7de2017c
7
- data.tar.gz: bdab25968892f155f6fb37e0ebb462a975db8476c36978572b01463561a57ec82b2363ca851357dfb7954c8094d47509fcb39a75e768f4a1878d93e3c8e3c478
6
+ metadata.gz: 2a3cbf2eb8168f8c56375ec18baa5bea255ce27e38f4703619509afd58e840af0b3fd884431ff69a034f87c3b1c330f20db5a4377c26d0743cf20d44a1f7403d
7
+ data.tar.gz: bcd1344c6424166090cb441f9631a7ba3dee033361d1f0d05dc6292d94fbeae5c35c4c5904f80a6646a045aa9d42ce0898c10d0bc21aee8018983e967cc59d24
@@ -1,32 +1,35 @@
1
1
  #!/usr/bin/env ruby
2
2
  #
3
- # Usage: puppet-masterless <subcommand> [<argument> ...] [-- puppet arguments ...]
3
+ # Usage: puppet-masterless <command> [<argument> ...] [-- puppet arguments ...]
4
4
  #
5
5
  # Applies a local puppet project to a remote host.
6
6
  #
7
- # Available subcommands:
7
+ # Available commands:
8
8
  #
9
- # package
10
- # apply
9
+ # apply - apply a project to a remote host
10
+ # package - package a project as a shell script
11
11
  #
12
12
  # Available arguments:
13
13
  #
14
- # confdir <directory>
15
- # hiera_config <file>
16
- # manifest <file>
17
- # modulepath <directory>
18
- # puppet <executable>
19
- # to <hostname>
20
- # as <username>
21
- # file <file>
14
+ # confdir <pathname> - main project directory
15
+ # manifest <filename> - manifest to apply
16
+ # hiera_config <filename> - passed to --hiera_config
17
+ # modulepath <pathname> - passed to --modulepath
18
+ # puppet <executable> - location of puppet executable
19
+ # to <hostname> - hostname of remote machine
20
+ # as <username> - name of user running puppet-apply
21
+ # file <filename> - include a file or directory
22
+ # output file <filename> - output filename
22
23
  #
23
- # At least <confdir> must be specified.
24
+ # At least "confdir" must be specified.
24
25
  #
25
26
 
27
+ require 'fileutils'
26
28
  require 'shellwords'
29
+ require 'securerandom'
27
30
 
28
31
  module PuppetMasterless
29
- ARCHIVE_NAME = 'puppet-masterless'
32
+ ARCHIVE_NAME = 'puppet-package'
30
33
  ARCHIVE_OPTIONS = '--ignore-failed-read --exclude-vcs'
31
34
  DEFAULT_PUPPET = 'puppet'
32
35
  REMOTE_USER = 'root'
@@ -40,19 +43,15 @@ module PuppetMasterless
40
43
  @puppet = DEFAULT_PUPPET
41
44
  @user = REMOTE_USER
42
45
  @workdir = REMOTE_WORKDIR
43
- @output = "#{ARCHIVE_NAME}.sh"
46
+ @output = "#{ARCHIVE_NAME}-#{SecureRandom.hex(4)}"
44
47
  end
45
48
 
46
- def banner
47
- File.read(__FILE__).lines[1..23].map { |s| s.gsub(/^# ?/, '') }
48
- end
49
-
50
- def usage n = 1
49
+ def usage(n = 1)
51
50
  STDERR.puts(banner)
52
51
  exit(n)
53
52
  end
54
53
 
55
- def collect args
54
+ def collect(args)
56
55
  while word = args.shift
57
56
  case word
58
57
  when 'with', 'and'
@@ -65,6 +64,7 @@ module PuppetMasterless
65
64
  when 'as' then @user = args.shift
66
65
  when 'file' then @files << args.shift
67
66
  when '--', nil then @args = args.slice!(0..-1)
67
+ when 'output' then collect_output(args)
68
68
  else fail 'Invalid argument: ' << word
69
69
  end
70
70
  end
@@ -73,16 +73,19 @@ module PuppetMasterless
73
73
  def validate
74
74
  fail 'No confdir specified' unless @confdir
75
75
  fail 'No puppet specified' unless @puppet
76
+ fail 'No output file specified' unless @output
76
77
 
77
78
  @manifest ||= File.join(@confdir, 'manifests')
78
79
  @modulepath ||= File.join(@confdir, 'modules')
79
80
  @hiera_config ||= File.join(@confdir, 'hiera.yaml')
81
+ @hieradata ||= File.join(@confdir, 'hieradata')
80
82
 
81
83
  fail 'No such confdir: ' << @confdir unless File.directory?(@confdir)
82
84
  fail 'No such manifest: ' << @manifest unless File.exist?(@manifest)
83
85
 
84
86
  @modulepath = nil unless File.directory?(@modulepath)
85
87
  @hiera_config = nil unless File.file?(@hiera_config)
88
+ @hieradata = nil unless File.directory?(@hieradata)
86
89
  end
87
90
 
88
91
  def package
@@ -90,22 +93,28 @@ module PuppetMasterless
90
93
  end
91
94
 
92
95
  def apply
93
- if @hostname
94
- create_distribution
95
- apply_remote
96
- else
97
- apply_local
98
- end
96
+ @hostname ? apply_remote : apply_local
99
97
  end
100
98
 
101
99
  private
102
100
 
101
+ def banner
102
+ File.read(__FILE__).lines[1..24].map { |s| s.gsub(/^# ?/, '') }
103
+ end
104
+
105
+ def collect_output(args)
106
+ usage(1) unless args.shift == 'file'
107
+ @output = args.shift
108
+ end
109
+
103
110
  def archive_command
104
- "tar -czf- #{ARCHIVE_OPTIONS} --transform 's|^|#{@path.gsub("'", "'\''")}/|' \
105
- #{@files.shelljoin} \
106
- #{@manifest.shellescape} \
107
- #{@modulepath.shellescape if @modulepath} \
108
- #{@hiera_config.shellescape if @hiera_config}"
111
+ command = "tar -cf- #{ARCHIVE_OPTIONS} --transform 's|^|#{@path.gsub("'", "'\''")}/|'"
112
+ command << ' ' << @files.shelljoin
113
+ command << ' ' << @manifest.shellescape
114
+ command << ' ' << @modulepath.shellescape if @modulepath
115
+ command << ' ' << @hiera_config.shellescape if @hiera_config
116
+ command << ' ' << @hieradata.shellescape if @hieradata
117
+ command << ' | gzip -f'
109
118
  end
110
119
 
111
120
  def local_apply_command
@@ -129,20 +138,24 @@ module PuppetMasterless
129
138
  end
130
139
 
131
140
  def apply_remote
141
+ STDERR.puts("Notice: Creating distribution")
142
+ create_distribution
143
+
132
144
  STDERR.puts('Notice: Copying to ' << @hostname)
133
145
  fail 'Copy command failed' unless system("scp -q #{@output.shellescape} #{@hostname.shellescape}:#{@workdir.shellescape}")
146
+
134
147
  STDERR.puts('Notice: Applying to ' << @hostname)
135
148
  fail 'Apply command failed' unless system("ssh -q -t #{@hostname.shellescape} #{remote_apply_command.shellescape}")
136
149
  ensure
150
+ FileUtils.rm_f(@output)
137
151
  system("ssh -q -t #{@hostname.shellescape} #{remote_cleanup_command.shellescape}")
138
152
  end
139
153
 
140
154
  def create_distribution
141
- STDERR.puts("Notice: Creating distribution")
142
-
143
155
  File.open(@output, 'w') do |o|
144
156
  o.puts("#!/bin/sh -e")
145
- o.puts("sed '0,/^# EOF$/d' \"$0\" | tar -xzf-")
157
+ o.puts("umask 077")
158
+ o.puts("sed '0,/^# EOF$/d' \"$0\" | gunzip -f | tar -xf-")
146
159
  o.puts("cd #{@path.shellescape}")
147
160
  o.puts(local_apply_command)
148
161
  o.puts("exit 1")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-masterless
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Hanson