Class: Bosh::Cli::Command::Stemcell

Inherits:
Base show all
Includes:
VersionCalc
Defined in:
lib/cli/commands/stemcell.rb

Constant Summary collapse

PUBLIC_STEMCELL_INDEX =

The filename of the public stemcell index.

"public_stemcells_index.yml"
PUBLIC_STEMCELL_INDEX_URL =

The URL of the public stemcell index.

"https://blob.cfblob.com/rest/objects/4e4e78bca2" +
"1e121204e4e86ee151bc04f6a19ce46b22?uid=bb6a0c89ef4048a8a0f814e2538" +
"5d1c5/user1&expires=1893484800&signature=NJuAr9c8eOid7dKFmOEN7bmzAlI="

Constants inherited from Base

Base::BLOBS_DIR, Base::BLOBS_INDEX_FILE

Instance Attribute Summary

Attributes inherited from Base

#cache, #config, #options, #out, #usage, #work_dir

Instance Method Summary collapse

Methods included from VersionCalc

#major_version, #minor_version, #version_cmp, #version_greater, #version_less, #version_same

Methods inherited from Base

#blob_manager, #blobstore, command, #confirmed?, #director, #dry_run?, #exit_code, #full_target_name, #initialize, #interactive?, #logged_in?, #non_interactive?, #redirect, #release, #run, #show_usage, #target_name, #target_version, #task_report, #verbose?

Constructor Details

This class inherits a constructor from Bosh::Cli::Command::Base

Instance Method Details

#delete(name, version) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/cli/commands/stemcell.rb', line 152

def delete(name, version)
  auth_required

  say("You are going to delete stemcell `#{name}/#{version}'".red)

  unless confirmed?
    say("Canceled deleting stemcell".green)
    return
  end

  status, _ = director.delete_stemcell(name, version)

  task_report(status, "Deleted stemcell `#{name}/#{version}'")
end

#download_public(stemcell_name) ⇒ Object

Downloads one of the publicly available stemcells.

Parameters:

  • stemcell_name (String)

    The name of the stemcell, as seen in the public stemcell index file.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cli/commands/stemcell.rb', line 123

def download_public(stemcell_name)
  yaml = get_public_stemcell_list
  yaml.delete(PUBLIC_STEMCELL_INDEX) if yaml.has_key?(PUBLIC_STEMCELL_INDEX)

  unless yaml.has_key?(stemcell_name)
    available_stemcells = yaml.map { |k| k }.join(", ")
    puts("'#{stemcell_name}' not found in '#{available_stemcells}'.".red)
    return
  end

  if File.exists?(stemcell_name) &&
      !agree("#{stemcell_name} exists locally. Overwrite it? [yn]")
    return
  end

  url = yaml[stemcell_name]["url"]
  size = yaml[stemcell_name]["size"]
  progress_bar = ProgressBar.new(stemcell_name, size)
  progress_bar.file_transfer_mode
  File.open("#{stemcell_name}", "w") { |file|
    @http_client.get(url) do |chunk|
      file.write(chunk)
      progress_bar.inc(chunk.size)
    end
  }
  progress_bar.finish
  puts("Download complete.")
end

#get_public_stemcell_listHash

Grabs the index file for the publicly available stemcells.

Returns:

  • (Hash)

    The index file YAML as a hash.



92
93
94
95
96
97
98
99
100
# File 'lib/cli/commands/stemcell.rb', line 92

def get_public_stemcell_list
  @http_client = HTTPClient.new
  response = @http_client.get(PUBLIC_STEMCELL_INDEX_URL)
  status_code = response.http_header.status_code
  if status_code != HTTP::Status::OK
    err("Received HTTP #{status_code} from #{PUBLIC_STEMCELL_INDEX_URL}.")
  end
  YAML.load(response.body)
end

#listObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cli/commands/stemcell.rb', line 67

def list
  auth_required
  stemcells = director.list_stemcells.sort do |sc1, sc2|
    sc1["name"] == sc2["name"] ?
        version_cmp(sc1["version"], sc2["version"]) :
        sc1["name"] <=> sc2["name"]
  end

  err("No stemcells") if stemcells.size == 0

  stemcells_table = table do |t|
    t.headings = "Name", "Version", "CID"
    stemcells.each do |sc|
      t << [sc["name"], sc["version"], sc["cid"]]
    end
  end

  say("\n")
  say(stemcells_table)
  say("\n")
  say("Stemcells total: %d" % stemcells.size)
end

#list_public(*args) ⇒ Object

Prints out the publicly available stemcells.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cli/commands/stemcell.rb', line 103

def list_public(*args)
  full = args.include?("--full")
  yaml = get_public_stemcell_list
  stemcells_table = table do |t|
    t.headings = "Name", "Url"
    yaml.keys.sort.each do |key|
      if key != PUBLIC_STEMCELL_INDEX
        url = full ? yaml[key]["url"] : "#{yaml[key]["url"][0..49]}..."
        t << [key, url]
      end
    end
  end
  puts(stemcells_table)
  puts("To download use 'bosh download public stemcell <stemcell_name>'." +
      "For full url use --full.")
end

#upload(tarball_path) ⇒ Object



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
# File 'lib/cli/commands/stemcell.rb', line 32

def upload(tarball_path)
  auth_required

  stemcell = Bosh::Cli::Stemcell.new(tarball_path, cache)

  say("\nVerifying stemcell...")
  stemcell.validate
  say("\n")

  unless stemcell.valid?
    err("Stemcell is invalid, please fix, verify and upload again")
  end

  say("Checking if stemcell already exists...")
  name = stemcell.manifest["name"]
  version = stemcell.manifest["version"]

  existing = director.list_stemcells.select do |sc|
    sc["name"] == name and sc["version"] == version
  end

  if existing.empty?
    say("No")
  else
    err("Stemcell \"#{name}\":\"#{version}\" already exists, " +
        "increment the version if it has changed")
  end

  say("\nUploading stemcell...\n")

  status, _ = director.upload_stemcell(stemcell.stemcell_file)

  task_report(status, "Stemcell uploaded and created")
end

#verify(tarball_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cli/commands/stemcell.rb', line 15

def verify(tarball_path)
  stemcell = Bosh::Cli::Stemcell.new(tarball_path, cache)

  say("\nVerifying stemcell...")
  stemcell.validate
  say("\n")

  if stemcell.valid?
    say("'%s' is a valid stemcell" % [tarball_path])
  else
    say("'%s' is not a valid stemcell:" % [tarball_path])
    for error in stemcell.errors
      say("- %s" % [error])
    end
  end
end