Skip to content

Commit

Permalink
refactor: download and extract using sn-releases crate
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jacderida committed Dec 12, 2023
1 parent 09bfda4 commit 58aa96f
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 12,256 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/artifacts/
/deploy/

/.vagrant/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ semver = "1.0.4"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
tar = "0.4"
sn-releases = { git = "https://github.com/jacderida/sn-releases", branch = "custom-url" }
tempfile = "3.8.1"
textwrap = "0.16.0"
tokio = { version = "1.26", features = ["full"] }

Expand All @@ -35,5 +36,6 @@ winreg = "0.7"
[dev-dependencies]
assert_fs = "~1.0"
assert_matches = "1.5.0"
httpmock = "0.6"
async-trait = "0.1"
mockall = "0.11.3"
predicates = "2.0"
64 changes: 64 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2204"
config.vm.provider :libvirt do |libvirt|
libvirt.memory = 4096
end
config.vm.synced_folder ".",
"/vagrant",
type: "9p",
accessmode: "mapped",
mount_options: ['rw', 'trans=virtio', 'version=9p2000.L']
config.vm.provision "file", source: "~/.ssh/id_rsa", destination: "/home/vagrant/.ssh/id_rsa"
config.vm.provision "shell", inline: "apt-get update -y"
config.vm.provision "shell", inline: "apt-get install -y build-essential"
config.vm.provision "shell", privileged: false, inline: <<-SHELL
curl -L -O https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init
chmod +x rustup-init
./rustup-init --default-toolchain stable --no-modify-path -y
echo "source ~/.cargo/env" >> ~/.bashrc
SHELL
config.vm.provision "shell", inline: <<-SHELL
curl -L -O https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init
chmod +x rustup-init
./rustup-init --default-toolchain stable --no-modify-path -y
echo "source ~/.cargo/env" >> ~/.bashrc
# Copy the binaries to a system-wide location for running tests as the root user
sudo cp ~/.cargo/bin/** /usr/local/bin
SHELL
config.vm.provision "shell", privileged: false, inline: <<-SHELL
mkdir -p ~/.vim/tmp/ ~/.vim/backup
cat <<'EOF' > ~/.vimrc
set nocompatible
let mapleader=" "
syntax on
set background=dark
set backspace=indent,eol,start
set backupdir=~/.vim/tmp//
set directory=~/.vim/backup
set expandtab
set foldlevel=1
set foldmethod=indent
set foldnestmax=10
set hlsearch
set ignorecase
set incsearch
set laststatus=2
set nobackup
set nofoldenable
set nowrap
set number relativenumber
set ruler
set shiftwidth=4
set smartindent
set showcmd
set shortmess+=A
set tabstop=4
set viminfo+=!
nnoremap j gj
nnoremap k gk
EOF
SHELL
end
8,821 changes: 0 additions & 8,821 deletions resources/releases_response_body.json

This file was deleted.

414 changes: 0 additions & 414 deletions resources/sn_cli_release_missing_asset_response_body.json

This file was deleted.

448 changes: 0 additions & 448 deletions resources/sn_cli_release_response_body.json

This file was deleted.

414 changes: 0 additions & 414 deletions resources/sn_node_release_missing_asset_response_body.json

This file was deleted.

448 changes: 0 additions & 448 deletions resources/sn_node_release_response_body.json

This file was deleted.

414 changes: 0 additions & 414 deletions resources/sn_testnet_release_missing_asset_response_body.json

This file was deleted.

448 changes: 0 additions & 448 deletions resources/sn_testnet_release_response_body.json

This file was deleted.

21 changes: 8 additions & 13 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::github::GithubReleaseRepository;
use crate::install::{AssetType, Settings};
use crate::s3::S3AssetRepository;
use crate::update::{perform_update_assessment, UpdateAssessmentResult};
use color_eyre::{eyre::eyre, Result};
use lazy_static::lazy_static;
use prettytable::{Cell, Row, Table};
use sn_releases::SafeReleaseRepositoryInterface;
use std::collections::HashMap;
use std::env::consts::{ARCH, OS};
use std::path::PathBuf;

const GITHUB_API_URL: &str = "https://api.github.com";
const ORG_NAME: &str = "maidsafe";
const REPO_NAME: &str = "safe_network";
const WRAP_LENGTH: usize = 80;

lazy_static! {
Expand Down Expand Up @@ -69,15 +65,15 @@ pub(crate) async fn process_install_cmd(
}

pub(crate) async fn process_update_cmd() -> Result<()> {
let platform = get_platform()?;
let safe_config_dir_path = get_safe_config_dir_path()?;
let settings_file_path = safe_config_dir_path.join("safeup.json");
let settings = Settings::read(&settings_file_path)?;
let release_repository = GithubReleaseRepository::new(GITHUB_API_URL, ORG_NAME, REPO_NAME);
let release_repo = <dyn SafeReleaseRepositoryInterface>::default_config();

for asset_type in AssetType::variants() {
println!("Retrieving latest version for {asset_type}...");
let (_, latest_version) = release_repository
.get_latest_asset_name(&asset_type, &platform)
let latest_version = release_repo
.get_latest_version(&asset_type.get_release_type())
.await?;
println!("Latest version of {asset_type} is {latest_version}");
if settings.is_installed(&asset_type) {
Expand All @@ -86,6 +82,7 @@ pub(crate) async fn process_update_cmd() -> Result<()> {
settings.get_installed_version(&asset_type)
);
}

let decision = perform_update_assessment(&asset_type, &latest_version, &settings)?;
match decision {
UpdateAssessmentResult::PerformUpdate => {
Expand Down Expand Up @@ -153,12 +150,10 @@ async fn do_install_binary(
version: Option<String>,
) -> Result<()> {
let platform = get_platform()?;
let asset_repository = S3AssetRepository::new(ASSET_TYPE_BUCKET_MAP[asset_type]);
let release_repository = GithubReleaseRepository::new(GITHUB_API_URL, ORG_NAME, REPO_NAME);
let release_repo = <dyn SafeReleaseRepositoryInterface>::default_config();
let (installed_version, bin_path) = crate::install::install_bin(
asset_type.clone(),
release_repository,
asset_repository,
release_repo,
&platform,
dest_dir_path.clone(),
version,
Expand Down
Loading

0 comments on commit 58aa96f

Please sign in to comment.