summaryrefslogtreecommitdiff
path: root/cmd/nodecfg/download.go
blob: 1be8fc4d76bdc9b9db2b6097def2cb2123fbbe5f (plain)
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
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"fmt"
	"os"
	"path"
	"path/filepath"

	"github.com/algorand/go-algorand/util/s3"
	"github.com/algorand/go-algorand/util/tar"
)

func downloadAndExtractConfigPackage(channel string, targetDir string, configBucket string) (err error) {
	fmt.Fprintf(os.Stdout, "Downloading latest configuration file for '%s'...\n", channel)
	packageFile, err := downloadConfigPackage(channel, targetDir, configBucket)
	if err != nil {
		return fmt.Errorf("error downloading config package for channel '%s': %v", channel, err)
	}

	// Extract package and update configFilename
	fmt.Fprintf(os.Stdout, "Expanding configuration package '%s' to %s\n", packageFile, targetDir)
	return extractConfigPackage(packageFile, targetDir)
}

func downloadConfigPackage(channelName string, targetDir string, configBucket string) (packageFile string, err error) {
	if configBucket == "" {
		configBucket = s3.GetS3ReleaseBucket()
	}
	fmt.Fprintf(os.Stdout, "Downloading configuration package for channel '%s' from bucket '%s'\n", channelName, configBucket)
	s3Session, err := s3.MakeS3SessionForDownloadWithBucket(configBucket)
	if err != nil {
		return
	}

	prefix := fmt.Sprintf("config_%s", channelName)
	version, name, err := s3Session.GetLatestPackageFilesVersion(channelName, prefix)
	if err != nil {
		return
	}
	if version == 0 {
		err = fmt.Errorf("no config package found for channel '%s'", channelName)
		return
	}

	fileName := path.Base(name)
	packageFile = filepath.Join(targetDir, fileName)
	file, err := os.Create(packageFile)
	if err != nil {
		return
	}
	defer file.Close()

	if err = s3Session.DownloadFile(name, file); err != nil {
		err = fmt.Errorf("error downloading file: %v", err)
		return
	}
	return
}

func extractConfigPackage(packageFile string, targetDir string) (err error) {
	err = tar.UncompressFile(packageFile, targetDir)
	if err != nil {
		return
	}
	return
}