Class: Librarian::Puppet::Simple::CLI

Inherits:
Thor
  • Object
show all
Includes:
Installer, Iterator, Util
Defined in:
lib/librarian/puppet/simple/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Iterator

#clear_modules, #each_module, #each_module_of_type, #mod, #modules

Methods included from Installer

#install!

Methods included from Util

#base_dir, #forge, #module_path, #print_verbose, #system_cmd

Class Method Details

.bin!Object



24
25
26
# File 'lib/librarian/puppet/simple/cli.rb', line 24

def self.bin!
  start
end

Instance Method Details

#cleanObject



66
67
68
69
70
# File 'lib/librarian/puppet/simple/cli.rb', line 66

def clean
  target_directory = options[:path] || File.expand_path("./modules")
  puts "Target Directory: #{target_directory}" if options[:verbose]
  FileUtils.rm_rf target_directory
end

#compare_reposObject



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
# File 'lib/librarian/puppet/simple/cli.rb', line 150

def compare_repos

  repo_hash = {}
  @verbose            = options[:verbose]
  abort('path not supported by compare_repos command') if options[:path]
  if options[:downstream_only] and options[:upstream_only]
    abort('Cannot specify both downstream_only and upstream_only')
  end

  # create path where code will be stored
  if options[:existing_tmp_dir]
    path = options[:existing_tmp_dir]
  else
    path = File.join('.tmp', Time.now.strftime("%Y_%m_%d_%H_%S"))
  end

  FileUtils.mkdir_p(path)
  @custom_module_path = path

  # install the downstream modules in our tmp directory and build out a hash
  downstream = build_puppetfile_hash('downstream', !options[:existing_tmp_dir])
  # just build a hash of the downstream modules
  upstream   = build_puppetfile_hash('upstream', false)

  unless ( (downstream.keys - upstream.keys) == [] and
           (upstream.keys - downstream.keys)
         )
    abort('Your Puppetfile did not produce the same upstream and downstream repos, this is not yet supported')
  else

    upstream.each do |us_name, us_repo|
      # compare to see if the source of revisions are the same
      ds_repo = downstream[us_name]
      if ds_repo[:git] == us_repo[:git] and ds_repo[:ref] == us_repo[:ref]
        print_verbose("\nSources of #{us_name} are the same, nothing to compare.")
      else
        Dir.chdir(File.join(path, us_name)) do
          if us_repo[:git] =~ /(git|https?):\/\/(.+)\/(.+)?\/(.+)/
            remote_name = $3
            remotes = system_cmd('git remote')
            if remotes.include?(remote_name)
              puts "Did not have to add remote #{remote_name} to #{us_repo[:name]}, it was already there"
            else
              puts "Adding remote #{remote_name} #{us_repo[:git]}"
              system_cmd("git remote add #{remote_name} #{us_repo[:git]}")
            end
            system_cmd("git fetch #{remote_name}")
            if us_repo[:ref] =~ /^origin\/(\S+)$/
              compare_ref = "#{remote_name}/#{$1}"
            else
              compare_ref = "#{remote_name}/#{us_repo[:ref]}"
            end

            # set up parameters for git log call
            ignore_merges = options[:ignore_merges] ? '--no-merges' : ''
            show_diffs    = options[:show_diffs]    ? '-u' : ''
            oneline       = options[:oneline]       ? '--oneline' : ''
            # show the results, this assumes that HEAD is up-to-date (which it should be)

            if options[:downstream_only] and options[:upstream_only]
              abort('Cannot specify both downstream_only and upstream_only')
            end
            puts "########## Results for #{us_name} ##########"
            unless options[:upstream_only]
              puts "  ######## Commits only in downstream ########"
              results = system_cmd("git log --left-only HEAD...#{compare_ref} #{ignore_merges} #{show_diffs} #{oneline}", true)
              puts "  ######## End Downstream results ########"
            end
            unless options[:downstream_only]
              puts "  ######## Commits only in upstream ########"
              results = system_cmd("git log --right-only HEAD...#{compare_ref} #{ignore_merges} #{show_diffs} #{oneline}", true)
              puts "  ######## End upstream ########"
            end
            puts "########## End of Results for #{us_name} ##########"
          else
            abort("Unrecognizable upstream url #{us_repo[:git]}")
          end
        end
      end
    end
  end
end

#dev_setup(remote_name) ⇒ Object



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
# File 'lib/librarian/puppet/simple/cli.rb', line 91

def dev_setup(remote_name)
  @custom_module_path = options[:path]
  # populate @modules
  eval(File.read(File.expand_path(options[:puppetfile])))
  each_module_of_type(:git) do |repo|
    Dir.chdir(File.join((options[:path] || 'modules'), repo[:name])) do
      print_verbose "Adding development remote for git repo #{repo[:name]}"
      remotes = system_cmd('git remote')
      if remotes.include?(remote_name)
        puts "Did not have to add remote #{remote_name} to #{repo[:name]}"
      elsif ! remotes.include?('origin')
        raise(TestException, "Repo #{repo[:name]} has no remote called origin, failing")
      else
        remote_url = system_cmd('git remote show origin').detect {|x| x =~ /\s+Push\s+URL: / }
        if remote_url =~ /(git|https?):\/\/(.+)\/(.+)?\/(.+)/
          url = "git@#{$2}:#{remote_name}/#{$4}"
          puts "Adding remote #{remote_name} as #{url}"
          system_cmd("git remote add #{remote_name} #{url}")
        elsif remote_url =~ /^git@/
          puts "Origin is already a read/write remote, skipping b/c this is unexpected"
        else
          puts "remote_url #{remote_url} did not have the expected format. weird..."
        end
      end
    end
  end
end

#generate_puppetfileObject



236
237
238
239
240
241
242
243
244
245
# File 'lib/librarian/puppet/simple/cli.rb', line 236

def generate_puppetfile
  eval(File.read(File.expand_path(options[:puppetfile])))
  if options[:out_file]
    File.open(options[:out_file], 'w') do |fh|
      print_puppet_file(fh)
    end
  else
    print_puppet_file(STDOUT)
  end
end

#git_statusObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/librarian/puppet/simple/cli.rb', line 73

def git_status
  @custom_module_path = options[:path]
  # populate @modules
  eval(File.read(File.expand_path(options[:puppetfile])))
  each_module_of_type(:git) do |repo|
    Dir.chdir(File.join(module_path, repo[:name])) do
      status = system_cmd('git status')
      if status.include?('nothing to commit (working directory clean)')
        puts "Module #{repo[:name]} has not changed" if options[:verbose]
      else
        puts "Uncommitted changes for: #{repo[:name]}"
        puts "  #{status.join("\n  ")}"
      end
    end
  end
end

#installObject



30
31
32
33
34
35
36
37
# File 'lib/librarian/puppet/simple/cli.rb', line 30

def install
  @verbose = options[:verbose]
  clean if options[:clean]
  @custom_module_path = options[:path]
  # evaluate the file to populate @modules
  eval(File.read(File.expand_path(options[:puppetfile])))
  install!
end

#updateObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/librarian/puppet/simple/cli.rb', line 41

def update
  @verbose = options[:verbose]
  @custom_module_path = options[:path]
  eval(File.read(File.expand_path(options[:puppetfile])))
  each_module_of_type(:git) do |repo|
    if Dir.exists?(File.join(module_path, repo[:name]))
      Dir.chdir(File.join(module_path, repo[:name])) do
        remote = repo[:git]
        # if no ref is given, assume master
        branch = repo[:ref] || 'master'
        if branch =~ /^origin\/(.*)$/
          branch = $1
        end
        co_cmd     = 'git checkout FETCH_HEAD'
        update_cmd = "git fetch #{repo[:git]} #{branch} && #{co_cmd}"
        print_verbose "\n\n#{repo[:name]} -- #{update_cmd}"
        git_pull_cmd = system_cmd(update_cmd)
      end
    else
      install_git module_path, repo[:name], repo[:git], repo[:ref]
    end
  end
end