Class: VastSd::Start

Inherits:
Object
  • Object
show all
Includes:
Vast
Defined in:
lib/vast-sd/start.rb

Instance Method Summary collapse

Methods included from Vast

#vast_cmd

Constructor Details

#initialize(config:) ⇒ Start

Returns a new instance of Start.



13
14
15
# File 'lib/vast-sd/start.rb', line 13

def initialize(config:)
  @config = config
end

Instance Method Details

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/vast-sd/start.rb', line 17

def run
  logger = TTY::Logger.new
  prompt = TTY::Prompt.new

   = vast_cmd("show", "user")
  logger.info "Credit balance", sprintf("$%2.2f", ["credit"])

  offers = vast_cmd("search", "offers")

  offers = offers.select do |offer|
    has_good_gpu =
      if @config["preferred_gpus"].to_a.count > 0
        @config["preferred_gpus"].include?(offer["gpu_name"])
      else
        true
      end

    has_good_gpu && offer["rentable"]
  end.sort_by do |offer|
    offer["dph_total"]
  end

  id = prompt.select("Instance selection", offers.map { |offer|
    {
      name: sprintf("%-40s %s", "#{offer['num_gpus']} #{offer["gpu_name"]} - #{offer["geolocation"]}", sprintf("$%2.2f/hr", offer["dph_total"])),
      value: offer["id"]
    }
  }, per_page: 20)

  contract = vast_cmd(
    "create",
    "instance", id,
    "--disk", 50,
    "--jupyter",
    "--jupyter-dir", "/",
    "--direct",
    "--env", "-e JUPYTER_DIR=/ -p 3000:3000",
    "--onstart-cmd", %{
      sed -i '/rsync -au --remove-source-files \/venv\/ \/workspace\/venv\//a source \/workspace\/venv\/bin\/activate\n pip install jupyter_core' /start.sh;
      /start.sh
    },
    "--image", "runpod/stable-diffusion:web-automatic-8.0.3"
  )

  contract_id = contract["new_contract"]

  at_exit do
    vast_cmd("destroy", "instance", contract_id)

    logger.success "Instance destroyed"
  end

  logger.success "Contract ID", contract_id

  # Wait until instance is ready
  TTY::Spinner.new("[:spinner] Waiting for instance: :status", clear: true).run do |spinner|
    loop do
      instance = vast_cmd("show", "instance", contract_id)

      spinner.update status: instance["actual_status"] || "Initialization"

      if instance["actual_status"] == "running"
        port = instance["ports"]["3000/tcp"][0]["HostPort"]

        logger.info "http://#{instance["public_ipaddr"]}:#{port}"

        break
      else
        sleep 5
      end
    end
  end

  ssh_info = vast_cmd('ssh-url', contract_id)
  logger.info ssh_info

  @config["models"].entries.each do |namespace, urls|
    urls.each do |url|
      TTY::Spinner.new("[:spinner] Loading \"#{File.basename(url)}\" in models/#{namespace}", clear: true).run do
        upload_cmd = "curl #{Shellwords.escape(url)} -o /workspace/stable-diffusion-webui/models/#{namespace}/#{File.basename(url)}"

        loop do
          system("ssh -o StrictHostKeyChecking=no -q #{Shellwords.escape(ssh_info.chomp)} #{Shellwords.escape(upload_cmd)} &> /dev/null")
          break if $? == 0
        end
      end
    end
  end

  logger.success "Instance ready"

  # Run watchdog
  spinner = TTY::Spinner.new("[:spinner] Running: :balance")
  spinner.auto_spin

  loop do
    begin
       = vast_cmd("show", "user")
      spinner.update balance: "credit balance #{sprintf("$%2.2f", user_info["credit"])}"

      instance = vast_cmd("show", "instance", contract_id)

      if instance["actual_status"] != "running"
        logger.error "Instance #{instance['actual_status']}"
        exit
      else
        sleep 60
      end
    rescue Interrupt
      exit(0)
    end
  end

  spinner.stop
end