Class: Chef::Knife::CookbookGithubInstall

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/cookbook_github_install.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cookbook_nameObject (readonly)

Returns the value of attribute cookbook_name.



59
60
61
# File 'lib/chef/knife/cookbook_github_install.rb', line 59

def cookbook_name
  @cookbook_name
end

#github_branchObject (readonly)

Returns the value of attribute github_branch.



58
59
60
# File 'lib/chef/knife/cookbook_github_install.rb', line 58

def github_branch
  @github_branch
end

#github_repoObject (readonly)

Returns the value of attribute github_repo.



57
58
59
# File 'lib/chef/knife/cookbook_github_install.rb', line 57

def github_repo
  @github_repo
end

#github_userObject (readonly)

Returns the value of attribute github_user.



56
57
58
# File 'lib/chef/knife/cookbook_github_install.rb', line 56

def github_user
  @github_user
end

Instance Method Details

#clear_existing_files(cookbook_path) ⇒ Object



130
131
132
133
# File 'lib/chef/knife/cookbook_github_install.rb', line 130

def clear_existing_files(cookbook_path)
  ui.info("Removing pre-existing version.")
  shell_out!("rm -r #{cookbook_path}", :cwd => @install_path) if File.directory?(cookbook_path)
end

#clone_cookbookObject



119
120
121
122
123
124
# File 'lib/chef/knife/cookbook_github_install.rb', line 119

def clone_cookbook
  shell_out!("rm -rf #{temp_clone_path}", :cwd => tmpdir) if File.exists?(File.join(tmpdir, temp_clone_path))
  shell_out!("git clone #{github_uri} #{temp_clone_path}", :cwd => tmpdir)
  shell_out!("git checkout #{github_branch}", :cwd => File.join(tmpdir, temp_clone_path))
  shell_out!("rm -rf .git", :cwd => File.join(tmpdir, temp_clone_path))
end

#github_uriObject



135
136
137
138
139
140
141
# File 'lib/chef/knife/cookbook_github_install.rb', line 135

def github_uri
  if config[:ssh] || @github_user == ENV['USER']
    "[email protected]:#{@github_user}/#{@github_repo}.git"
  else
    "git://github.com/#{@github_user}/#{@github_repo}.git"
  end
end

#move_cookbookObject



126
127
128
# File 'lib/chef/knife/cookbook_github_install.rb', line 126

def move_cookbook
  shell_out!("mv #{temp_clone_path} #{File.join(@install_path, @cookbook_name)}", :cwd => tmpdir)
end

#parse_name_args!Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chef/knife/cookbook_github_install.rb', line 93

def parse_name_args!
  if name_args.empty?
    ui.error("please specify a cookbook to download and install")
    exit 1
  elsif name_args.size > 1
    ui.error("Usage: knife cookbook github install USER/REPO [USER/REPO/BRANCH] (options)")
    exit 1
  else
    @github_user, @github_repo, @github_branch = name_args.first.split('/')
    unless @github_user && @github_repo
      ui.error("Expected a github user and a repo to download from: jnewland/chef_ipmi")
      exit 1
    end
    @cookbook_name = @github_repo.gsub(/[_-]?chef(?!-client|-server|_handler)[-_]?/, '').
                                  gsub(/[_-]?cookbook[-_]?/, '')
  end
end

#runObject



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
# File 'lib/chef/knife/cookbook_github_install.rb', line 61

def run
  extend Chef::Mixin::ShellOut

  if config[:cookbook_path]
    Chef::Config[:cookbook_path] = config[:cookbook_path]
  else
    config[:cookbook_path] = Chef::Config[:cookbook_path]
  end

  parse_name_args!

  @install_path = File.expand_path(Array(config[:cookbook_path]).first)
  ui.info "Installing #@cookbook_name from #{github_uri} to #{@install_path}"

  @repo = CookbookSCMRepoExtensions.new(@install_path, ui, config)

  @repo.sanity_check
  @repo.reset_to_default_state
  @repo.prepare_to_import(@cookbook_name)

  clone_cookbook
  clear_existing_files(File.join(@install_path, @cookbook_name))
  move_cookbook

  if @repo.finalize_updates_from_github(@cookbook_name, "#{@github_user}/#{@github_repo}", sha)
    @repo.reset_to_default_state
    @repo.merge_updates_from(@cookbook_name, sha)
  else
    @repo.reset_to_default_state
  end
end

#shaObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/chef/knife/cookbook_github_install.rb', line 143

def sha
  @sha ||= begin
    data = nil
    raise "Unable to find SHA checksum" unless ['heads','tags'].any? do |ref_type|
      begin
        data = noauth_rest.get_rest("https://api.github.com/repos/#{@github_user}/#{@github_repo}/git/refs/#{ref_type}/#{github_branch}")
        ui.info("Found #{github_branch} amoung #{ref_type}.")
        true
      rescue
        ui.info("Unable to find #{github_branch} amoung #{ref_type}.")
        false
      end
    end
    data['object']['sha']
  end
end

#temp_clone_pathObject



111
112
113
# File 'lib/chef/knife/cookbook_github_install.rb', line 111

def temp_clone_path
  "_tmp_chef_#{@cookbook_name}"
end

#tmpdirObject



115
116
117
# File 'lib/chef/knife/cookbook_github_install.rb', line 115

def tmpdir
  Dir.tmpdir
end