Class: KnifeFlow::Increment

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

Constant Summary collapse

WORKING_BRANCH =
"develop"

Instance Method Summary collapse

Instance Method Details

#check_branch(name) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/chef/knife/increment.rb', line 93

def check_branch(name)
  if (`git status` =~ /#{name}/) != nil
    return true
  else
    ui.error("USAGE: you must be in the #{name} branch")
    exit 1
  end
end

#commit_and_push_branch(branch, comment) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/chef/knife/increment.rb', line 78

def commit_and_push_branch(branch, comment)
  print "--------------------------------- \n"
  system("git pull origin #{branch}") 
  system("git add .")
  system("git commit -am  '#{comment}'")
  system("git push origin #{branch}")
  print "--------------------------------- \n"
end

#find_version(name) ⇒ Object



102
103
104
105
# File 'lib/chef/knife/increment.rb', line 102

def find_version(name)
 loader = Chef::CookbookLoader.new(@cookbook_path)
 return loader[name].version
end

#increment_version(version) ⇒ Object



107
108
109
110
111
# File 'lib/chef/knife/increment.rb', line 107

def increment_version(version)
 current_version = version.split(".").map{|i| i.to_i}
 current_version[2] = current_version[2] + 1
 return current_version.join('.')
end

#parse_name_args!Object



69
70
71
72
73
74
75
76
# File 'lib/chef/knife/increment.rb', line 69

def parse_name_args!
  if name_args.empty?
    ui.error("USAGE: knife increment COOKBOOK COOKBOOK COOKBOOK")
    exit 1
  else
    return name_args
  end
end

#pull_branch(name) ⇒ Object



87
88
89
90
91
# File 'lib/chef/knife/increment.rb', line 87

def pull_branch(name)
  print "--------------------------------- \n"
  system("git pull origin #{name}")
  print "--------------------------------- \n"
end

#replace_version(search_string, replace_string, file) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/chef/knife/increment.rb', line 113

def replace_version(search_string, replace_string, file)
  open_file = File.open(file, "r")
  body_of_file = open_file.read
  open_file.close
  body_of_file.gsub!(search_string, replace_string)
  File.open(file, "w") { |file| file << body_of_file }
end

#runObject



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

def run
 
  @cookbooks = parse_name_args!

  self.config = Chef::Config.merge!(config)
  
  if !config[:cookbook_path]
    raise ArgumentError, "Default cookbook_path is not specified in the knife.rb config file, and a value to -o is not provided. Nowhere to write the new cookbook to." 
  end
  
  if check_branch(WORKING_BRANCH)

    pull_branch(WORKING_BRANCH) 

    @cookbook_path = Array(config[:cookbook_path]).first
 
    @cookbooks.each do | book |
       = File.join(@cookbook_path, book, "metadata.rb")

      # 1) increase version on the metadata file
      replace_version(find_version(book), increment_version(find_version(book)),  )
    
    end
  
    # 2) upload cookbooks to chef server
    cookbook_up = Chef::Knife::CookbookUpload.new
    cookbook_up.name_args = @cookbooks 
    cookbook_up.config[:freeze] = true
    cookbook_up.run  
    
    # 3) commit and push WORKING_BRANCH
    commit_and_push_branch(WORKING_BRANCH, "#{@cookbooks} have been incremented")
  
  end

end