/
create.go
186 lines (166 loc) · 5.18 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright © 2020 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"time"
"github.com/avast/retry-go"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ovrclk/akash/provider/cluster"
"github.com/ovrclk/akash/provider/gateway"
dcli "github.com/ovrclk/akash/x/deployment/client/cli"
pmodule "github.com/ovrclk/akash/x/provider"
pquery "github.com/ovrclk/akash/x/provider/query"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
func init() {
rootCmd.AddCommand(createCmd())
}
// createCmd represents the create command
func createCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create [sdl-file]",
Args: cobra.ExactArgs(1),
Short: "Create a deployment to be managed by the deploy application",
RunE: func(cmd *cobra.Command, args []string) error {
log := logger.With("cli", "create")
dd, err := NewDeploymentData(args[0], cmd.Flags(), config.GetAccAddress())
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
group, _ := errgroup.WithContext(ctx)
// Listen to on chain events and send the manifest when required
group.Go(func() error {
if err = ChainEmitter(ctx, DeploymentDataUpdateHandler(dd), SendManifestHander(dd)); err != nil {
log.Error("error watching events", err)
}
return err
})
// Store the deployment manifest in the archive
group.Go(func() error {
if err = config.CreateDeploymentFileInArchive(dd); err != nil {
log.Error("error updating archive", err)
}
return err
})
// Send the deployment creation transaction
group.Go(func() error {
if err = config.TxCreateDeployment(dd); err != nil {
log.Error("error creating deployment", err)
}
return err
})
// Wait for the leases to be created and then start polling the provider for service availability
group.Go(func() error {
if err = config.WaitForLeasesAndPollService(dd, cancel); err != nil {
log.Error("error listening for service", err)
}
return err
})
return group.Wait()
},
}
dcli.AddDeploymentIDFlags(cmd.Flags())
return cmd
}
// WaitForLeasesAndPollService waits for
func (c *Config) WaitForLeasesAndPollService(dd *DeploymentData, cancel context.CancelFunc) error {
log := logger
pclient := pmodule.AppModuleBasic{}.GetQueryClient(config.CLICtx(config.NewTMClient()))
timeout := time.After(90 * time.Second)
tick := time.Tick(500 * time.Millisecond)
for {
select {
case <-timeout:
log.Info("timed out (90s) listening for deployment to be available")
cancel()
return nil
case <-tick:
if dd.ExpectedLeases() {
for _, l := range dd.Leases() {
var (
p *pquery.Provider
err error
)
if err := retry.Do(func() error {
p, err = pclient.Provider(l.Provider)
if err != nil {
// TODO: Log retry?
return err
}
return nil
}); err != nil {
cancel()
return fmt.Errorf("error querying provider: %w", err)
}
// TODO: Move to using service status here?
var ls *cluster.LeaseStatus
if err := retry.Do(func() error {
ls, err = gateway.NewClient().LeaseStatus(context.Background(), p.HostURI, l)
if err != nil {
return err
}
return nil
}); err != nil {
cancel()
return fmt.Errorf("error querying lease status: %w", err)
}
for _, s := range ls.Services {
// TODO: Much better logging/ux could be put in here: waiting, timeouts etc...
if s.Available == s.Total {
log.Info(strings.Join(s.URIs, ","), "name", s.Name, "available", s.Available)
cancel()
return nil
}
}
}
}
}
}
}
// CreateDeploymentFileInArchive creates the deployment file in the `$HOME/.akash-deploy/deployments/` folder
func (c *Config) CreateDeploymentFileInArchive(dd *DeploymentData) error {
fileName := fmt.Sprintf("%s.%d.yaml", dd.DeploymentID.Owner, dd.DeploymentID.DSeq)
depDir := path.Join(homePath, "deployments")
if _, err := os.Stat(depDir); os.IsNotExist(err) {
if err = os.MkdirAll(depDir, 0777); err != nil {
return err
}
}
return ioutil.WriteFile(path.Join(depDir, fileName), dd.SDLFile, 644)
}
// TxCreateDeployment takes DeploymentData and creates the specified deployment
func (c *Config) TxCreateDeployment(dd *DeploymentData) (err error) {
res, err := c.SendMsgs([]sdk.Msg{dd.MsgCreate()})
log := logger.With(
"hash", res.TxHash,
"code", res.Code,
"codespace", res.Codespace,
"action", "create-deployment",
"dseq", dd.DeploymentID.DSeq,
)
if err != nil || res.Code != 0 {
log.Error("tx failed")
return err
}
log.Info("tx sent successfully")
return nil
}