Class: KnifeGithubRepoDestroy::GithubRepoDestroy

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

Instance Method Summary collapse

Instance Method Details

#create_cookbook(name, tmp) ⇒ Object

Create the cookbook template for upload

Parameters:

  • name (String)

    cookbook name tmp [String] temp location



198
199
200
201
202
203
# File 'lib/chef/knife/github_repo_destroy.rb', line 198

def create_cookbook(name, tmp)
  args = [ name ]
  create = Chef::Knife::CookbookCreate.new(args)
  create.config[:cookbook_path] = tmp
  create.run
end

#delete_request(url, token) ⇒ Object

Send DELETE command to API OAuth authentication token from config or command line

Parameters:

  • url (String)

    target url (organization or user) body [JSON] json data with repo configuration token [String] token sring



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/chef/knife/github_repo_destroy.rb', line 229

def delete_request(url, token)

  # if @github_ssl_verify_mode == "verify_none"
  #   config[:ssl_verify_mode] = :verify_none
  # elsif @github_ssl_verify_mode == "verify_peer"
  #   config[:ssl_verify_mode] = :verify_peer
  # end

  Chef::Log.debug("URL: " + url.to_s)

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host,uri.port)
  if uri.scheme == "https"
    http.use_ssl = true
    if @github_ssl_verify_mode == "verify_none"
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    else
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end
  end
   
  req = Net::HTTP::Delete.new(uri.path, initheader = {"Authorization" => "token #{token}"}) 
  req.body = get_body_json()        
  response = http.request(req)
  
  return nil if response.code == "204"

  unless response.code == "201" then
    puts "Error #{response.code}: #{response.message}"
    puts JSON.pretty_generate(JSON.parse(response.body))
    puts "URL: #{url}"
    exit 1
  end

  begin
    json = JSON.parse(response.body)
  rescue
    ui.warn "The result on the RESTRequest is not in json format"
    ui.warn "Output: " + response.body
    exit 1
  end
  json
end

#get_body_jsonObject

Create the json body with repo config for POST information

Parameters:

  • name (String)

    cookbook name



207
208
209
210
211
# File 'lib/chef/knife/github_repo_destroy.rb', line 207

def get_body_json()
  body = {
    "scopes" => ["public_repo"]
  }.to_json
end

#get_github_tokenObject

Get the OAuth authentication token from config or command line

Parameters:

  • nil


215
216
217
218
219
220
221
222
223
# File 'lib/chef/knife/github_repo_destroy.rb', line 215

def get_github_token()
  token = locate_config_value('github_token')
  if token.nil? || token.empty?
    Chef::Log.error("Cannot find any token information!")
    Chef::Log.error("Please use: knife github token create")
    exit 1
  end
  token
end

#get_useremailObject

Get the email from passwd file or git config

Parameters:

  • nil


176
177
178
179
180
181
# File 'lib/chef/knife/github_repo_destroy.rb', line 176

def get_useremail()
  email = nil
  git_user_email = %x(git config user.email).strip
  email = git_user_email if git_user_email
  email
end

#get_userloginObject

Get the email from passwd file or git config

Parameters:

  • nil


185
186
187
188
189
190
191
192
# File 'lib/chef/knife/github_repo_destroy.rb', line 185

def get_userlogin()
  email = get_useremail()
  unless email
    puts "Cannot continue without login information. Please define the git email address."
    exit 1
  end
   = email.split('@').first
end

#get_usernameObject

Get the username from passwd file or git config

Parameters:

  • nil


165
166
167
168
169
170
171
172
# File 'lib/chef/knife/github_repo_destroy.rb', line 165

def get_username()
  username = ENV['USER']
  passwd_user = %x(getent passwd #{username} | cut -d ':' -f 5).chomp
  username = passwd_user if passwd_user
  git_user_name = %x(git config user.name).strip
  username = git_user_name if git_user_name
  username
end

#git_commit_and_push(cookbook_path, github_ssh_url) ⇒ Object

Set the username in README.md

Parameters:

  • cookbook_path (String)

    cookbook path github_ssh_url [String] github ssh url from repo



125
126
127
128
129
130
131
# File 'lib/chef/knife/github_repo_destroy.rb', line 125

def git_commit_and_push(cookbook_path, github_ssh_url)
  shell_out!("git init", :cwd => cookbook_path )
  shell_out!("git add .", :cwd => cookbook_path ) 
  shell_out!("git commit -m 'creating initial cookbook structure from the knife-github plugin' ", :cwd => cookbook_path ) 
  shell_out!("git remote add origin #{github_ssh_url} ", :cwd => cookbook_path ) 
  shell_out!("git push -u origin master", :cwd => cookbook_path ) 
end

#runObject



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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef/knife/github_repo_destroy.rb', line 57

def run
  extend Chef::Mixin::ShellOut

  # validate base options from base module.
  validate_base_options      

  # Display information if debug mode is on.
  display_debug_info

  # Get the repo name from the command line
  name = name_args.first

  # Get the organization name from config
  org = locate_config_value('github_organizations').first

  if name.nil? || name.empty? 
    Chef::Log.error("Please specify a repository name")
    exit 1
  end 
   
  user = get_userlogin

  if config[:github_user_repo]
    url = @github_url + "/api/" + @github_api_version + "/repos/#{user}/#{name}"
    Chef::Log.debug("Destroying repository in user environment: #{user}")
  else
    url = @github_url + "/api/" + @github_api_version + "/repos/#{org}/#{name}"
    Chef::Log.debug("Destroying repository in organization: #{org}")
  end

  # @github_tmp = locate_config_value("github_tmp") || '/var/tmp/gitcreate'
  # @github_tmp = "#{@github_tmp}#{Process.pid}"

  # Get token information
  token = get_github_token()

  # Get body data for post
  # body = get_body_json(name, desc)

  # Creating the local repository 
  # Chef::Log.debug("Creating the local repository based on template")
  # create_cookbook(name, @github_tmp)

  # cookbook_path = File.join(@github_tmp, name)

  # Updating README.md if needed.
  # update_readme(cookbook_path)
 
  # Updateing metadata.rb if needed.
  # update_metadata(cookbook_path)

  # Creating the github repository
  repo = delete_request(url, token)
  puts "Repo: #{name} is deleted" if repo.nil?

  # github_ssh_url = repo['ssh_url']

  # Chef::Log.debug("Commit and push local repository")      
  # Initialize the local git repo
  # git_commit_and_push(cookbook_path, github_ssh_url)

  # Chef::Log.debug("Removing temp files")
  # FileUtils.remove_entry(@github_tmp)
end

#update_metadata(cookbook_path) ⇒ Object

Set the username and email in metadata.rb

Parameters:

  • name (String)

    cookbook path



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/chef/knife/github_repo_destroy.rb', line 149

def (cookbook_path)
  contents = ''
  username = get_username
  email    = get_useremail
   = File.join(cookbook_path, "metadata.rb")
  File.foreach() do |line|
    line.gsub!(/YOUR_COMPANY_NAME/,username) if username
    line.gsub!(/YOUR_EMAIL/,email) if email
    contents = contents << line
  end
  File.open(, 'w') {|f| f.write(contents) }
  return nil
end

#update_readme(cookbook_path) ⇒ Object

Set the username in README.md

Parameters:

  • name (String)

    cookbook path



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/chef/knife/github_repo_destroy.rb', line 135

def update_readme(cookbook_path)
  contents = ''
  username = get_username
  readme = File.join(cookbook_path, "README.md")
  File.foreach(readme) do |line|
    line.gsub!(/TODO: List authors/,"#{username}\n")
    contents = contents << line
  end
  File.open(readme, 'w') {|f| f.write(contents) }
  return nil
end