Class: StackFu::Commands::PublishCommand

Inherits:
Command
  • Object
show all
Includes:
ApiHooks
Defined in:
lib/stackfu/commands/publish_command.rb

Constant Summary

Constants included from Rendering

Rendering::LEFT_MARGIN

Constants included from OperatingSystems

OperatingSystems::FriendlyNames, OperatingSystems::OperatingSystems

Instance Attribute Summary

Attributes inherited from Command

#errors, #options, #parameters, #subcommand

Instance Method Summary collapse

Methods included from ApiHooks

#initialize_api

Methods inherited from Command

#command, command_for, create, inherited, #initialize, #params?, #run, #valid?

Methods included from Rendering

#done, #error, #fill_values_from_options, #menu_for, #render_target, #spinner, #table, #warning

Methods included from OperatingSystems

#convert_os, #os_name

Constructor Details

This class inherits a constructor from StackFu::Commands::Command

Instance Method Details

#default(parameters, options) ⇒ Object



29
30
31
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/stackfu/commands/publish_command.rb', line 29

def default(parameters, options)
  what = :script
  
  unless what
    error "Couldn't find an item to publish on current folder.",
      "Make sure you have a file named 'stack.yml', 'plugin.yml' or use 'stackfu generate' for creating a new stack."
  end
  
  silently do
    begin
      stack_spec = read_and_validate(what)
    
      if @errors.keys.any?
        error format_error
        return
      end
  
      %w[controls requirements executions validations].each_with_index do |item, i|
        if (yaml = read("config/0#{i+1}-#{item}.yml"))
          yaml.gsub!("type:", "_type:")
          if (from_yaml = YAML.load(yaml))
            if item == 'requirements' or item == 'validations'
              buffer = []
              from_yaml[item].each do |itm|
                itm["params"] = { "data" => itm.delete("data") }
                buffer << itm
              end
              from_yaml[item] = buffer
            end
          
            stack_spec[item == "scripts" ? "executions" : item] = from_yaml[item]
          end
        end
      end
    
      unless stack_spec["executions"].present?
        error "To publish a #{what} you have to define at least one execution.",
          "Take a look at the executions descriptor file config/03-executions.yml for more information.\nYou can also use 'stackfu generate stack_name script_name:script' command to auto-generate a sample execution."
        return false
      end
  
      return unless stack_spec["executions"].each do |script|
        template = "executables/#{script["file"]}.sh.erb"
    
        begin
          script["body"] = read(template)
        rescue Errno::ENOENT
          error "The template file for the script '#{script["description"]}' was not found.", "This script has an executable called '#{script["description"]}', and the template for it should be in a file called executables/#{script["file"]}.sh.erb."
          break false
        end
    
        true
      end
    
      item_class = StackFu::ApiHooks.const_get("#{what.to_s.classify}")
      item_class.format = :json

      begin
        stack = item_class.find(stack_spec["name"])
      rescue ActiveResource::ResourceNotFound 
      rescue NoMethodError
        if $!.message =~ /closed\?/
          raise Errno::ECONNREFUSED
        else
          raise
        end
      end

      if stack
        unless options[:update]
          if agree("You already have a #{what} named #{stack_spec["name"]}. Do you want to update it?")
            puts ""
            puts "Tip: Next time you can avoid this question using 'stack pub --update'."
            puts ""
          else
            puts "Aborted."
            return false
          end
        end
    
        begin
          item_class.delete(stack.name)
        rescue ActiveResource::ResourceNotFound 
          puts "There was a problem updating your #{what}. Please report this problem at [email protected] or try again in a few minutes."
          return
        end
      end
  
      puts "Publishing #{what} #{stack_spec["name"]}..."

      stack = item_class.new(stack_spec)

      if publish(stack)
        done "#{what.to_s.titleize} #{stack.name} published."
      else
        error "Could not publish your stack: #{stack.errors.full_messages.to_s}"
      end
    rescue ActiveResource::ServerError
      error "#{$!.message}"
    rescue Errno::ENOENT
      error "There was an error opening your file descriptor"
      raise
    end
  end
end

#format_errorObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/stackfu/commands/publish_command.rb', line 16

def format_error
  messages = ["there were validation problems with your script.", ""]
  %w(script.yml config/01-controls.yml config/02-requirements.yml config/03-executions.yml config/04-validations.yml).each do |f|
    if @errors[f]
      messages << "Errors in #{f.foreground(:green)}:"
      messages << @errors[f].map { |e| "- #{e}" }
      messages << ""
    end
  end
  
  messages.flatten.join("\n")
end

#plugin?Boolean

Returns:



12
13
14
# File 'lib/stackfu/commands/publish_command.rb', line 12

def plugin?
  File.exists?("plugin.yml")
end

#stack?Boolean

Returns:



8
9
10
# File 'lib/stackfu/commands/publish_command.rb', line 8

def stack?
  File.exists?("stack.yml")
end