Class: Whisk::Provider::Bowl

Inherits:
Whisk::Provider show all
Includes:
Mixin::ShellOut
Defined in:
lib/whisk/provider/bowl.rb

Instance Attribute Summary

Attributes inherited from Whisk::Provider

#resource

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#run_command, #run_command!, #shell_out, #shell_out!

Methods inherited from Whisk::Provider

#action_nothing

Constructor Details

#initialize(resource) ⇒ Bowl

Returns a new instance of Bowl.



30
31
32
33
# File 'lib/whisk/provider/bowl.rb', line 30

def initialize(resource)
  super
  @environment = nil
end

Instance Method Details

#action_destroyObject



76
77
78
79
80
81
82
83
84
# File 'lib/whisk/provider/bowl.rb', line 76

def action_destroy
  if self.exist?
    ingredients_run("destroy")
    if Dir.entries(resource.path) == ["..", "."]
      Whisk.ui.info("Destroying empty bowl #{resource.name}")
      Dir.unlink(resource.path)
    end
  end
end

#action_diffObject



86
87
88
89
90
91
# File 'lib/whisk/provider/bowl.rb', line 86

def action_diff
  if self.exist?
    ::Dir.chdir resource.path
    ingredients_run("diff")
  end
end

#action_listObject



93
94
95
96
97
98
99
# File 'lib/whisk/provider/bowl.rb', line 93

def action_list
  if self.exist?
    resource.ingredients.each do |name, ingredient|
      Whisk.ui.info("#{resource.name}/#{ingredient.name}")
    end
  end
end

#action_prepareObject



101
102
103
104
105
106
# File 'lib/whisk/provider/bowl.rb', line 101

def action_prepare
  self.create unless self.exist?
  ::Dir.chdir resource.path
  Whisk.ui.info "Preparing bowl '#{resource.name}' with path #{resource.path}"
  ingredients_run("prepare")
end

#action_statusObject



108
109
110
111
112
113
114
# File 'lib/whisk/provider/bowl.rb', line 108

def action_status
  if self.exist?
    ::Dir.chdir resource.path
    Whisk.ui.info "Status for bowl '#{resource.name}' with path #{resource.path}"
    ingredients_run("status")
  end
end

#action_updateObject



116
117
118
119
120
121
# File 'lib/whisk/provider/bowl.rb', line 116

def action_update
  if self.exist?
    ::Dir.chdir resource.path
    ingredients_run("update")
  end
end

#action_uploadObject



123
124
125
126
127
128
129
# File 'lib/whisk/provider/bowl.rb', line 123

def action_upload
  if self.exist?
    Whisk.ui.info "Uploading ingredients in bowl '#{resource.name}'"
    cookbooks = resource.ingredients.to_a.map {|name, cb| name}
    shell_out!("knife cookbook upload #{cookbooks.join(' ')}", :env => knife_env)
  end
end

#createObject



69
70
71
72
73
74
# File 'lib/whisk/provider/bowl.rb', line 69

def create
  unless self.exist?
    Whisk.ui.info "Creating bowl '#{resource.name}' with path #{resource.path}"
    ::FileUtils.mkdir_p resource.path
  end
end

#environmentObject



35
36
37
38
39
40
41
42
43
# File 'lib/whisk/provider/bowl.rb', line 35

def environment
  if resource.environment
    unless @environment.is_a? Chef::Environment
      env_json = run_command!("knife environment show -F json #{resource.environment}").stdout
      @environment = Chef::JSONCompat.from_json(env_json)
    end
  end
  @environment
end

#exist?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/whisk/provider/bowl.rb', line 45

def exist?
  ::Dir.exist? resource.path
end

#ingredients_run(action) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/whisk/provider/bowl.rb', line 49

def ingredients_run(action)
  resource.ingredients.each do |name, ingredient|
    if ingredient.ref == :ref_from_environment
      if environment and environment.cookbook_versions.has_key? ingredient.name
        ingredient.ref environment.cookbook_versions[ingredient.name]
      else
        Whisk.ui.warn "Cookbook version for ingredient #{name} not found in environment #{resource.environment}"
      end
    end
    ingredient.run_action(action)
  end
end

#knife_envObject



62
63
64
65
66
67
# File 'lib/whisk/provider/bowl.rb', line 62

def knife_env
  cb_path = resource.path
  return {
    'WHISK_COOKBOOK_PATH' => cb_path
  }
end