Class: KnifeSpork::Plugins::Git

Inherits:
Plugin
  • Object
show all
Defined in:
lib/knife-spork/plugins/git.rb

Instance Method Summary collapse

Methods inherited from Plugin

#enabled?, hook, hooks, #initialize, name

Constructor Details

This class inherits a constructor from KnifeSpork::Plugins::Plugin

Instance Method Details

#after_bumpObject



169
170
171
172
173
# File 'lib/knife-spork/plugins/git.rb', line 169

def after_bump
  cookbooks.each do |cookbook|
    git_add(cookbook_path_for(cookbook),"metadata.rb")
  end
end

#after_environmentcreateObject



72
73
74
75
76
# File 'lib/knife-spork/plugins/git.rb', line 72

def after_environmentcreate
  if config.auto_push
    save_environment(object_name) unless object_difference == ''
  end
end

#after_environmentdeleteObject



96
97
98
99
100
# File 'lib/knife-spork/plugins/git.rb', line 96

def after_environmentdelete
  if config.auto_push
    delete_environment(object_name)
  end
end

#after_environmenteditObject



86
87
88
89
90
# File 'lib/knife-spork/plugins/git.rb', line 86

def after_environmentedit
  if config.auto_push
    save_environment(object_name) unless object_difference == ''
  end
end

#after_nodecreateObject



112
113
114
115
116
# File 'lib/knife-spork/plugins/git.rb', line 112

def after_nodecreate
  if config.auto_push
    save_node(object_name) unless object_difference == ''
  end
end

#after_nodedeleteObject



136
137
138
139
140
# File 'lib/knife-spork/plugins/git.rb', line 136

def after_nodedelete
  if config.auto_push
    delete_node(object_name)
  end
end

#after_nodeeditObject



126
127
128
129
130
# File 'lib/knife-spork/plugins/git.rb', line 126

def after_nodeedit
  if config.auto_push
    save_node(object_name) unless object_difference == ''
  end
end

#after_promote_localObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/knife-spork/plugins/git.rb', line 175

def after_promote_local
  environments.each do |environment|
    git_add(environment_path,"#{environment}.json")
  end
  if config.auto_push
    branch =  if not config.branch.nil?
                config[:branch] 
              else 
                "master"
              end

    git_commit(environment_path, "promote #{cookbooks.collect{ |c| "#{c.name}@#{c.version}" }.join(",")} to #{environments.join(",")}")
    git_push(branch)
  end
end

#after_rolecreateObject



24
25
26
27
28
29
30
31
32
# File 'lib/knife-spork/plugins/git.rb', line 24

def after_rolecreate
  if config.auto_push
    if !File.directory?(role_path)
      ui.error "Role path #{role_path} does not exist"
      exit 1
    end
    save_role(object_name) unless object_difference == ''
  end
end

#after_roledeleteObject



52
53
54
55
56
# File 'lib/knife-spork/plugins/git.rb', line 52

def after_roledelete
  if config.auto_push
    delete_role(object_name)
  end
end

#after_roleeditObject



42
43
44
45
46
# File 'lib/knife-spork/plugins/git.rb', line 42

def after_roleedit
  if config.auto_push
    save_role(object_name) unless object_difference == ''
  end
end

#before_bumpObject



142
143
144
145
146
147
148
149
# File 'lib/knife-spork/plugins/git.rb', line 142

def before_bump
  git_pull(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","")
  git_pull_submodules(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","")
  cookbooks.each do |cookbook|
    git_pull(cookbook_path_for(cookbook))
    git_pull_submodules(cookbook_path_for(cookbook))
  end
end

#before_environmentcreateObject

Environmental Git wrappers



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/knife-spork/plugins/git.rb', line 59

def before_environmentcreate
  if config.auto_push
    if !File.directory?(environment_path)
      ui.error "Environment path #{environment_path} does not exist"
      exit 1
    end
    git_pull(environment_path)
    if File.exist?(File.join(environment_path, object_name + '.json'))
      ui.error 'Environment already exists in local git, aborting creation'
      exit 1
    end
  end
end

#before_environmentdeleteObject



91
92
93
94
95
# File 'lib/knife-spork/plugins/git.rb', line 91

def before_environmentdelete
  if config.auto_push
    git_pull(environment_path)
  end
end

#before_environmenteditObject



77
78
79
80
81
82
83
84
85
# File 'lib/knife-spork/plugins/git.rb', line 77

def before_environmentedit
  if config.auto_push
    git_pull(environment_path)
    if !File.exist?(File.join(environment_path, object_name + '.json'))
      ui.error 'Environment does not exist in git, please create it first with spork'
      exit 1
    end
  end
end

#before_nodecreateObject

Node Git wrappers



103
104
105
106
107
108
109
110
111
# File 'lib/knife-spork/plugins/git.rb', line 103

def before_nodecreate
  if config.auto_push
    git_pull(node_path)
    if File.exist?(File.join(node_path, object_name + '.json'))
      ui.error 'Node already exists in local git, aborting creation'
      exit 1
    end
  end
end

#before_nodedeleteObject



131
132
133
134
135
# File 'lib/knife-spork/plugins/git.rb', line 131

def before_nodedelete
  if config.auto_push
    git_pull(node_path)
  end
end

#before_nodeeditObject



117
118
119
120
121
122
123
124
125
# File 'lib/knife-spork/plugins/git.rb', line 117

def before_nodeedit
  if config.auto_push
    git_pull(node_path)
    if !File.exist?(File.join(node_path, object_name + '.json'))
      ui.error 'Node does not exist in git, please bootstrap one first'
      exit 1
    end
  end
end

#before_promoteObject



160
161
162
163
164
165
166
167
# File 'lib/knife-spork/plugins/git.rb', line 160

def before_promote
  cookbooks.each do |cookbook|
    git_pull(environment_path) unless cookbook_path_for(cookbook).include?(environment_path.gsub"/environments","")
    git_pull_submodules(environment_path) unless cookbook_path_for(cookbook).include?(environment_path.gsub"/environments","")
    git_pull(cookbook_path_for(cookbook))
    git_pull_submodules(cookbook_path_for(cookbook))
  end
end

#before_rolecreateObject

Role Git wrappers



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/knife-spork/plugins/git.rb', line 11

def before_rolecreate
  if config.auto_push
    if !File.directory?(role_path)
      ui.error "Role path #{role_path} does not exist"
      exit 1
    end
    git_pull(role_path)
    if File.exist?(File.join(role_path, object_name + '.json'))
      ui.error 'Role already exists in local git, aborting creation'
      exit 1
    end
  end
end

#before_roledeleteObject



47
48
49
50
51
# File 'lib/knife-spork/plugins/git.rb', line 47

def before_roledelete
  if config.auto_push
    git_pull(role_path)
  end
end

#before_roleeditObject



33
34
35
36
37
38
39
40
41
# File 'lib/knife-spork/plugins/git.rb', line 33

def before_roleedit
  if config.auto_push
    git_pull(role_path)
    if !File.exist?(File.join(role_path, object_name + '.json'))
      ui.error 'Role does not exist in git, please create it first with spork'
      exit 1
    end
  end
end

#before_uploadObject



151
152
153
154
155
156
157
158
# File 'lib/knife-spork/plugins/git.rb', line 151

def before_upload
  git_pull(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","")
  git_pull_submodules(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","")
  cookbooks.each do |cookbook|
    git_pull(cookbook_path_for(cookbook))
    git_pull_submodules(cookbook_path_for(cookbook))
  end
end

#delete_environment(environment) ⇒ Object



229
230
231
232
233
# File 'lib/knife-spork/plugins/git.rb', line 229

def delete_environment(environment)
  git_rm(environment_path, "#{environment}.json")
  git_commit(environment_path, "[ENV] Deleted #{environment}")
  git_push(branch) if config.auto_push
end

#delete_node(node) ⇒ Object



199
200
201
202
203
# File 'lib/knife-spork/plugins/git.rb', line 199

def delete_node(node)
  git_rm(node_path, "#{node}.json")
  git_commit(node_path, "[NODE] Deleted #{node}")
  git_push(branch) if config.auto_push
end

#delete_role(role) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/knife-spork/plugins/git.rb', line 213

def delete_role(role)
  git_rm(role_path, "#{role}.json")
  if config.auto_push
    git_commit(role_path, "[ROLE] Deleted #{role}")
    git_push(branch)
  end
end

#performObject



8
# File 'lib/knife-spork/plugins/git.rb', line 8

def perform; end

#save_environment(environment) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/knife-spork/plugins/git.rb', line 221

def save_environment(environment)
  json = JSON.pretty_generate(Chef::Environment.load(environment))
  environment_file = File.expand_path( File.join(environment_path, "#{environment}.json") )
  File.open(environment_file, 'w'){ |f| f.puts(json) }
  git_add(environment_path, "#{environment}.json")
  git_commit(environment_path, "[ENV] Updated #{environment}")
  git_push(branch) if config.auto_push
end

#save_node(node) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/knife-spork/plugins/git.rb', line 191

def save_node(node)
  json = JSON.pretty_generate(Chef::Node.load(node))
  node_file = File.expand_path( File.join(node_path, "#{node}.json") )
  File.open(node_file, 'w'){ |f| f.puts(json) }
  git_add(node_path, "#{node}.json")
  git_commit(node_path, "[NODE] Updated #{node}")
  git_push(branch) if config.auto_push
end

#save_role(role) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/knife-spork/plugins/git.rb', line 205

def save_role(role)
  json = JSON.pretty_generate(Chef::Role.load(role))
  role_file = File.expand_path( File.join(role_path, "#{role}.json") )
  File.open(role_file, 'w'){ |f| f.puts(json) }
  git_add(role_path, "#{role}.json")
  git_commit(role_path, "[ROLE] Updated #{role}")
  git_push(branch)
end