Skip to content

Commit f79bd4b

Browse files
committed
refactor: download and extract using sn-releases crate
The code for downloading and extracting release binaries from Github and S3 was being repeated in several different places, so we decided to put some common code for it in a crate. We are now updating `safeup` to use it, which removes both the `github` and `s3` modules, and makes the `install_bin` tests simpler. From the user's point of view, functionally everything should be the same. I also added a quick Vagrantfile here that can be used for testing safeup. This is useful if you don't want your local machine littered with binaries that were only used for testing.
1 parent 09bfda4 commit f79bd4b

15 files changed

+315
-12256
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/artifacts/
55
/deploy/
66

7+
/.vagrant/
78
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
89
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
910
Cargo.lock

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ semver = "1.0.4"
2525
serde = "1.0"
2626
serde_derive = "1.0"
2727
serde_json = "1.0"
28-
tar = "0.4"
28+
sn-releases = { git = "https://github.com/jacderida/sn-releases", branch = "custom-url" }
29+
tempfile = "3.8.1"
2930
textwrap = "0.16.0"
3031
tokio = { version = "1.26", features = ["full"] }
3132

@@ -35,5 +36,6 @@ winreg = "0.7"
3536
[dev-dependencies]
3637
assert_fs = "~1.0"
3738
assert_matches = "1.5.0"
38-
httpmock = "0.6"
39+
async-trait = "0.1"
40+
mockall = "0.11.3"
3941
predicates = "2.0"

Vagrantfile

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Vagrant.configure("2") do |config|
2+
config.vm.box = "generic/ubuntu2204"
3+
config.vm.provider :libvirt do |libvirt|
4+
libvirt.memory = 4096
5+
end
6+
config.vm.synced_folder ".",
7+
"/vagrant",
8+
type: "9p",
9+
accessmode: "mapped",
10+
mount_options: ['rw', 'trans=virtio', 'version=9p2000.L']
11+
config.vm.provision "file", source: "~/.ssh/id_rsa", destination: "/home/vagrant/.ssh/id_rsa"
12+
config.vm.provision "shell", inline: "apt-get update -y"
13+
config.vm.provision "shell", inline: "apt-get install -y build-essential"
14+
config.vm.provision "shell", privileged: false, inline: <<-SHELL
15+
curl -L -O https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init
16+
chmod +x rustup-init
17+
./rustup-init --default-toolchain stable --no-modify-path -y
18+
echo "source ~/.cargo/env" >> ~/.bashrc
19+
SHELL
20+
config.vm.provision "shell", inline: <<-SHELL
21+
curl -L -O https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init
22+
chmod +x rustup-init
23+
./rustup-init --default-toolchain stable --no-modify-path -y
24+
echo "source ~/.cargo/env" >> ~/.bashrc
25+
# Copy the binaries to a system-wide location for running tests as the root user
26+
sudo cp ~/.cargo/bin/** /usr/local/bin
27+
SHELL
28+
config.vm.provision "shell", privileged: false, inline: <<-SHELL
29+
mkdir -p ~/.vim/tmp/ ~/.vim/backup
30+
cat <<'EOF' > ~/.vimrc
31+
set nocompatible
32+
33+
let mapleader=" "
34+
syntax on
35+
36+
set background=dark
37+
set backspace=indent,eol,start
38+
set backupdir=~/.vim/tmp//
39+
set directory=~/.vim/backup
40+
set expandtab
41+
set foldlevel=1
42+
set foldmethod=indent
43+
set foldnestmax=10
44+
set hlsearch
45+
set ignorecase
46+
set incsearch
47+
set laststatus=2
48+
set nobackup
49+
set nofoldenable
50+
set nowrap
51+
set number relativenumber
52+
set ruler
53+
set shiftwidth=4
54+
set smartindent
55+
set showcmd
56+
set shortmess+=A
57+
set tabstop=4
58+
set viminfo+=!
59+
60+
nnoremap j gj
61+
nnoremap k gk
62+
EOF
63+
SHELL
64+
end

resources/releases_response_body.json

Lines changed: 0 additions & 8821 deletions
This file was deleted.

resources/sn_cli_release_missing_asset_response_body.json

Lines changed: 0 additions & 414 deletions
This file was deleted.

resources/sn_cli_release_response_body.json

Lines changed: 0 additions & 448 deletions
This file was deleted.

resources/sn_node_release_missing_asset_response_body.json

Lines changed: 0 additions & 414 deletions
This file was deleted.

resources/sn_node_release_response_body.json

Lines changed: 0 additions & 448 deletions
This file was deleted.

resources/sn_testnet_release_missing_asset_response_body.json

Lines changed: 0 additions & 414 deletions
This file was deleted.

resources/sn_testnet_release_response_body.json

Lines changed: 0 additions & 448 deletions
This file was deleted.

src/cmd.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66
// KIND, either express or implied. Please review the Licences for the specific language governing
77
// permissions and limitations relating to use of the SAFE Network Software.
88

9-
use crate::github::GithubReleaseRepository;
109
use crate::install::{AssetType, Settings};
11-
use crate::s3::S3AssetRepository;
1210
use crate::update::{perform_update_assessment, UpdateAssessmentResult};
1311
use color_eyre::{eyre::eyre, Result};
1412
use lazy_static::lazy_static;
1513
use prettytable::{Cell, Row, Table};
14+
use sn_releases::SafeReleaseRepositoryInterface;
1615
use std::collections::HashMap;
1716
use std::env::consts::{ARCH, OS};
1817
use std::path::PathBuf;
1918

20-
const GITHUB_API_URL: &str = "https://api.github.com";
21-
const ORG_NAME: &str = "maidsafe";
22-
const REPO_NAME: &str = "safe_network";
2319
const WRAP_LENGTH: usize = 80;
2420

2521
lazy_static! {
@@ -69,15 +65,15 @@ pub(crate) async fn process_install_cmd(
6965
}
7066

7167
pub(crate) async fn process_update_cmd() -> Result<()> {
72-
let platform = get_platform()?;
7368
let safe_config_dir_path = get_safe_config_dir_path()?;
7469
let settings_file_path = safe_config_dir_path.join("safeup.json");
7570
let settings = Settings::read(&settings_file_path)?;
76-
let release_repository = GithubReleaseRepository::new(GITHUB_API_URL, ORG_NAME, REPO_NAME);
71+
let release_repo = <dyn SafeReleaseRepositoryInterface>::default_config();
72+
7773
for asset_type in AssetType::variants() {
7874
println!("Retrieving latest version for {asset_type}...");
79-
let (_, latest_version) = release_repository
80-
.get_latest_asset_name(&asset_type, &platform)
75+
let latest_version = release_repo
76+
.get_latest_version(&asset_type.get_release_type())
8177
.await?;
8278
println!("Latest version of {asset_type} is {latest_version}");
8379
if settings.is_installed(&asset_type) {
@@ -86,6 +82,7 @@ pub(crate) async fn process_update_cmd() -> Result<()> {
8682
settings.get_installed_version(&asset_type)
8783
);
8884
}
85+
8986
let decision = perform_update_assessment(&asset_type, &latest_version, &settings)?;
9087
match decision {
9188
UpdateAssessmentResult::PerformUpdate => {
@@ -153,12 +150,10 @@ async fn do_install_binary(
153150
version: Option<String>,
154151
) -> Result<()> {
155152
let platform = get_platform()?;
156-
let asset_repository = S3AssetRepository::new(ASSET_TYPE_BUCKET_MAP[asset_type]);
157-
let release_repository = GithubReleaseRepository::new(GITHUB_API_URL, ORG_NAME, REPO_NAME);
153+
let release_repo = <dyn SafeReleaseRepositoryInterface>::default_config();
158154
let (installed_version, bin_path) = crate::install::install_bin(
159155
asset_type.clone(),
160-
release_repository,
161-
asset_repository,
156+
release_repo,
162157
&platform,
163158
dest_dir_path.clone(),
164159
version,

0 commit comments

Comments
 (0)