Module: Arfor::GithubModule

Defined in:
lib/arfor/github_module.rb

Constant Summary collapse

PUPPET_METADATA =
'metadata.json'
README_FILE =
'README.md'
README_ERB =
"../../res/puppet_module/#{README_FILE}.erb"

Class Method Summary collapse

Class Method Details

.createObject



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
# File 'lib/arfor/github_module.rb', line 27

def self.create()

  # http://stackoverflow.com/a/2889747/3441106
  forge_name  = ask("#{Arfor::QUESTION} Forge module name: ") do |q|
    q.validate = /[\w_]+-[\w_]+/
  end
  description = ask("#{Arfor::QUESTION} Module description: ")

  forge_name_split  = forge_name.split('-')
  forge_user        = forge_name_split[0]
  module_name       = forge_name_split[1]

  git_name    = "puppet-#{module_name}"

  git_opts    = {
    :description => description,
  }

  # Step 1 - generate puppet module
  if system("puppet module generate --skip-interview #{forge_name}")

    Dir.chdir(module_name) {

      # temporary ;-) fix for needing to replace the Gemfile
      File.delete('Gemfile')

      # Step 2 - install PDQtest
      system("pdqtest init")

      # Step 3 - make remote git repo
      begin
        resp = Arfor::Util::Github::create_repository(git_name, git_opts)
        full_name = resp.full_name
      rescue Octokit::UnprocessableEntity => e
        raise "Repository creation error:  #{e.message}"
      end

      # Step 4 - doco template
      template  = File.read(File.join(File.dirname(File.expand_path(__FILE__)), README_ERB))
      content   = ERB.new(template, nil, '-').result(binding)
      File.write(README_FILE, content)

      # Step 5 - checkout local copy of git repo, merge changes and upload
      system("git init && git remote add origin #{resp.clone_url}")

      # Step 6 - update puppet metdata.json with settings from git
       = JSON.parse(File.read(PUPPET_METADATA))
      ['source']      = resp.clone_url
      ['project_url'] = resp.clone_url
      ['issues_url']  = resp.issues_url
      ['summary']     = description
      File.write(PUPPET_METADATA, JSON.pretty_generate())

      # Step 7 - cleanup backup crud files
      Dir.glob("**/*.pdqtest_old").each { |f|
        File.delete(f)
      }

      # Step 8 - Enable travis (you need to have already logged yourself in)
      # the sleep is to allow time for the apis to settle before travis does
      # its scans
      puts "sleep for sync"
      sleep(30)
      system("travis enable --no-interactive")

      # Step 9 - initial git commit and upload changes
      system("git add -A && git commit -m 'initial' && git push origin master")
    }
  else
    raise "module generation error"
  end

  Arfor::OK
end