-
Notifications
You must be signed in to change notification settings - Fork 6
/
deploy.go
252 lines (210 loc) · 7.6 KB
/
deploy.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-License-Identifier: GPL-3.0
package main
import (
"fmt"
"math/big"
"path/filepath"
"github.com/comrade-coop/apocryph/pkg/abi"
"github.com/comrade-coop/apocryph/pkg/ethereum"
"github.com/comrade-coop/apocryph/pkg/ipcr"
tpipfs "github.com/comrade-coop/apocryph/pkg/ipfs"
pbcon "github.com/comrade-coop/apocryph/pkg/proto/protoconnect"
"github.com/comrade-coop/apocryph/pkg/publisher"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ipfs/kubo/client/rpc"
"github.com/spf13/cobra"
)
// if no provider is selected, Fetches providers based on registry args
func fetchAndFilterProviders(ipfs *rpc.HttpApi, ethClient *ethclient.Client) (publisher.ProviderHostInfoList, error) {
registryContract := common.HexToAddress(registryContractAddress)
var tokenContract common.Address
if tokenContractAddress != "" {
tokenContract = common.HexToAddress(tokenContractAddress)
} else if paymentContractAddress != "" {
paymentContract := common.HexToAddress(paymentContractAddress)
payment, err := abi.NewPayment(paymentContract, ethClient)
if err != nil {
return nil, fmt.Errorf("Failed instantiating payment contract: %w", err)
}
tokenContract, err = payment.Token(&bind.CallOpts{})
if err != nil {
return nil, fmt.Errorf("Failed getting payment contract token: %w", err)
}
} else {
return nil, fmt.Errorf("Either a token contract or a payment contract must be set")
}
tables, err := publisher.GetPricingTables(ethClient, registryContract, tokenContract)
if err != nil {
return nil, err
}
if len(tables) == 0 {
return nil, fmt.Errorf("Marketplace is empty! are you sure you are connected to the right contract?")
}
filter, err := getRegistryTableFilter()
if err != nil {
return nil, err
}
filteredTables := publisher.FilterPricingTables(tables, filter)
if len(filteredTables) == 0 {
return nil, fmt.Errorf("no table found by filter")
}
err = PrintTableInfo(filteredTables, ethClient, registryContract)
if err != nil {
return nil, err
}
availableProviders, err := publisher.GetProviderHostInfos(ipfs, ethClient, registryContract, filteredTables)
if err != nil {
return nil, err
}
availableProviders, err = publisher.FilterProviderHostInfos(region, providerPeer, availableProviders)
if err != nil {
return nil, err
}
PrintProvidersInfo(availableProviders)
return availableProviders, nil
}
// FIXME: Massive code duplication with registry get, upload, fund, and sync commands.
var deployPodCmd = &cobra.Command{
Use: fmt.Sprintf("deploy [%s] [deployment.yaml]", publisher.DefaultPodFile),
Aliases: []string{"update"},
Short: "Deploy a pod from a local manifest",
Args: cobra.MaximumNArgs(2),
GroupID: "main",
RunE: func(cmd *cobra.Command, args []string) error {
podFile, deploymentFile, pod, deployment, err := publisher.ReadPodAndDeployment(args, manifestFormat, deploymentFormat)
if err != nil {
return err
}
configureDeployment(deployment)
fundsInt, _ := (&big.Int{}).SetString(funds, 10)
if fundsInt == nil {
return fmt.Errorf("Invalid number passed for funds: %s", funds)
}
unlockTimeInt := big.NewInt(unlockTime)
ipfs, ipfsMultiaddr, err := tpipfs.GetIpfsClient(ipfsApi)
if err != nil {
return fmt.Errorf("Failed connecting to IPFS: %w", err)
}
ipfsp2p := tpipfs.NewP2pApi(ipfs, ipfsMultiaddr)
ethClient, err := ethereum.GetClient(ethereumRpc)
if err != nil {
return err
}
if publisherKey == "" {
publisherKey = common.BytesToAddress(deployment.Payment.PublisherAddress).String()
}
publisherAuth, sign, err := ethereum.GetAccountAndSigner(publisherKey, ethClient)
if err != nil {
return fmt.Errorf("Could not get ethereum account: %w", err)
}
// FIXME: move to configureDeployment?
chainId, err := ethClient.ChainID(cmd.Context())
if err != nil {
return err
}
deployment.Payment.ChainID = chainId.Bytes()
deployment.Payment.PublisherAddress = publisherAuth.From.Bytes()
interceptor := pbcon.NewAuthInterceptorClient(deployment, expirationOffset, sign)
var client *publisher.P2pProvisionPodServiceClient
if len(deployment.GetProvider().GetEthereumAddress()) == 0 || deployment.GetProvider().GetLibp2PAddress() == "" {
availableProviders, err := fetchAndFilterProviders(ipfs, ethClient)
if err != nil {
return fmt.Errorf("Failed finding a provider: %w", err)
}
if len(availableProviders) == 0 {
return fmt.Errorf("Failed finding a provider: no available providers found matching filter")
}
client, err = publisher.SetFirstConnectingProvider(ipfsp2p, availableProviders, deployment, interceptor)
if err != nil {
return fmt.Errorf("Failed setting a provider: %w", err)
}
} else {
client, err = publisher.ConnectToProvider(ipfsp2p, deployment, interceptor)
if err != nil {
return err
}
}
if uploadSecrets {
err = publisher.UploadSecrets(cmd.Context(), ipfs, filepath.Dir(podFile), pod, deployment)
if err != nil {
return err
}
}
ctrdClient, err := ipcr.GetContainerdClient("k8s.io")
if err != nil {
return err
}
if uploadImages {
err = publisher.UploadImages(cmd.Context(), ctrdClient, ipfsApi, pod, deployment)
if err != nil {
return err
}
}
err = publisher.SaveDeployment(deploymentFile, deploymentFormat, deployment) // Checkpoint uploads and keys so far
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "warning: %v\n", err)
}
err = publisher.FundPaymentChannel(ethClient, publisherAuth, deployment, fundsInt, unlockTimeInt, debugMintFunds)
if err != nil {
return err
}
err = publisher.SendToProvider(cmd.Context(), ipfsp2p, pod, deployment, client)
if err != nil {
return err
}
return publisher.SaveDeployment(deploymentFile, deploymentFormat, deployment)
},
}
var deletePodCmd = &cobra.Command{
Use: fmt.Sprintf("delete [%s|deployment.yaml]", publisher.DefaultPodFile),
Aliases: []string{"undeploy"},
Short: "Delete a pod from a local deployment",
Args: cobra.MaximumNArgs(1),
GroupID: "main",
RunE: func(cmd *cobra.Command, args []string) error {
_, deploymentFile, _, deployment, err := publisher.ReadPodAndDeployment(args, manifestFormat, deploymentFormat)
if err != nil {
return err
}
configureDeployment(deployment)
ipfs, ipfsMultiaddr, err := tpipfs.GetIpfsClient(ipfsApi)
if err != nil {
return fmt.Errorf("Failed connecting to IPFS: %w", err)
}
ethClient, err := ethereum.GetClient(ethereumRpc)
if err != nil {
return err
}
if publisherKey == "" {
publisherKey = common.BytesToAddress(deployment.Payment.PublisherAddress).String()
}
_, sign, err := ethereum.GetAccountAndSigner(publisherKey, ethClient)
if err != nil {
return fmt.Errorf("Could not get ethereum account: %w", err)
}
interceptor := pbcon.NewAuthInterceptorClient(deployment, expirationOffset, sign)
ipfsp2p := tpipfs.NewP2pApi(ipfs, ipfsMultiaddr)
client, err := publisher.ConnectToProvider(ipfsp2p, deployment, interceptor)
if err != nil {
return err
}
err = publisher.SendToProvider(cmd.Context(), tpipfs.NewP2pApi(ipfs, ipfsMultiaddr), nil, deployment, client)
if err != nil {
return err
}
return publisher.SaveDeployment(deploymentFile, deploymentFormat, deployment)
},
}
func init() {
podCmd.AddCommand(deployPodCmd)
podCmd.AddCommand(deletePodCmd)
deployPodCmd.Flags().AddFlagSet(deploymentFlags)
deployPodCmd.Flags().AddFlagSet(uploadFlags)
deployPodCmd.Flags().AddFlagSet(fundFlags)
deployPodCmd.Flags().AddFlagSet(syncFlags)
deployPodCmd.Flags().AddFlagSet(registryFlags)
deletePodCmd.Flags().AddFlagSet(deploymentFlags)
deletePodCmd.Flags().AddFlagSet(syncFlags)
}