Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

set more detailed version information during builds #58

Merged
merged 5 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/agent/onefuzz-agent/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
Expand All @@ -6,7 +7,7 @@ use std::process::Command;
fn run_cmd(args: &[&str]) -> Result<String, Box<dyn Error>> {
let cmd = Command::new(args[0]).args(&args[1..]).output()?;
if cmd.status.success() {
Ok(String::from_utf8_lossy(&cmd.stdout).to_string())
Ok(String::from_utf8_lossy(&cmd.stdout).trim().to_string())
} else {
Err(From::from("failed"))
}
Expand All @@ -16,21 +17,43 @@ fn read_file(filename: &str) -> Result<String, Box<dyn Error>> {
let mut file = File::open(filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
contents = contents.trim().to_string();

Ok(contents)
}

fn main() -> Result<(), Box<dyn Error>> {
fn print_version(include_sha: bool) -> Result<(), Box<dyn Error>> {
let mut version = read_file("../../../CURRENT_VERSION")?;
let sha = run_cmd(&["git", "rev-parse", "HEAD"])?;
let with_changes = if run_cmd(&["git", "diff", "--quiet"]).is_err() {
"-local_changes"
} else {
""
};
println!("cargo:rustc-env=GIT_VERSION={}{}", sha, with_changes);

let version = read_file("../../../CURRENT_VERSION")?;
if include_sha {
version.push('-');
version.push_str(&sha);

// if we're a non-release buil, check to see if git has
// unstaged changes
if run_cmd(&["git", "diff", "--quiet"]).is_err() {
version.push('.');
version.push_str("localchanges");
}
}

println!("cargo:rustc-env=GIT_VERSION={}", sha);
println!("cargo:rustc-env=ONEFUZZ_VERSION={}", version);

Ok(())
}

fn main() -> Result<(), Box<dyn Error>> {
// If we're built off of a tag, we accept CURRENT_VERSION as is. Otherwise
// modify it to indicate local build
if let Ok(val) = env::var("GITHUB_REF") {
if val.starts_with("refs/tags/") {
print_version(false)
} else {
print_version(true)
}
} else {
print_version(true)
}
}
41 changes: 32 additions & 9 deletions src/agent/onefuzz-supervisor/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
Expand All @@ -6,7 +7,7 @@ use std::process::Command;
fn run_cmd(args: &[&str]) -> Result<String, Box<dyn Error>> {
let cmd = Command::new(args[0]).args(&args[1..]).output()?;
if cmd.status.success() {
Ok(String::from_utf8_lossy(&cmd.stdout).to_string())
Ok(String::from_utf8_lossy(&cmd.stdout).trim().to_string())
} else {
Err(From::from("failed"))
}
Expand All @@ -16,21 +17,43 @@ fn read_file(filename: &str) -> Result<String, Box<dyn Error>> {
let mut file = File::open(filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
contents = contents.trim().to_string();

Ok(contents)
}

fn main() -> Result<(), Box<dyn Error>> {
fn print_version(include_sha: bool) -> Result<(), Box<dyn Error>> {
let mut version = read_file("../../../CURRENT_VERSION")?;
let sha = run_cmd(&["git", "rev-parse", "HEAD"])?;
let with_changes = if run_cmd(&["git", "diff", "--quiet"]).is_err() {
"-local_changes"
} else {
""
};
println!("cargo:rustc-env=GIT_VERSION={}{}", sha, with_changes);

let version = read_file("../../../CURRENT_VERSION")?;
if include_sha {
version.push('-');
version.push_str(&sha);

// if we're a non-release buil, check to see if git has
// unstaged changes
if run_cmd(&["git", "diff", "--quiet"]).is_err() {
version.push('.');
version.push_str("localchanges");
}
}

println!("cargo:rustc-env=GIT_VERSION={}", sha);
println!("cargo:rustc-env=ONEFUZZ_VERSION={}", version);

Ok(())
}

fn main() -> Result<(), Box<dyn Error>> {
// If we're built off of a tag, we accept CURRENT_VERSION as is. Otherwise
// modify it to indicate local build
if let Ok(val) = env::var("GITHUB_REF") {
if val.starts_with("refs/tags/") {
print_version(false)
} else {
print_version(true)
}
} else {
print_version(true)
}
}
34 changes: 27 additions & 7 deletions src/api-service/dev-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,32 @@ fi
set -ex

TARGET=${1}
cd ${APP_DIR}
(cd ../pytypes && python setup.py sdist bdist_wheel && cp dist/*.whl ../api-service/__app__)
cd __app__
pushd ${APP_DIR}
VERSION=$(../ci/get-version.sh)
../ci/set-versions.sh

# clean up any previously built onefuzztypes packages
rm __app__/onefuzztypes*.whl

# build a local copy of onefuzztypes
rm -rf local-pytypes
cp -r ../pytypes local-pytypes
pushd local-pytypes
rm -f dist/*
python setup.py sdist bdist_wheel
cp dist/*.whl ../__app__
popd
rm -r local-pytypes

# deploy a the instance with the locally built onefuzztypes
pushd __app__
uuidgen > onefuzzlib/build.id
sed -i s,onefuzztypes==0.0.0,./onefuzztypes-0.0.0-py3-none-any.whl, requirements.txt
TYPELIB=$(ls onefuzztypes*.whl)
sed -i s,.*onefuzztypes.*,./${TYPELIB}, requirements.txt
func azure functionapp publish ${TARGET} --python
sed -i s,./onefuzztypes-0.0.0-py3-none-any.whl,onefuzztypes==0.0.0, requirements.txt
rm 'onefuzztypes-0.0.0-py3-none-any.whl'
cat onefuzzlib/build.id
sed -i s,./onefuzztypes.*,onefuzztypes==0.0.0, requirements.txt
rm onefuzztypes*.whl
popd

../ci/unset-versions.sh
cat __app__/onefuzzlib/build.id
28 changes: 28 additions & 0 deletions src/ci/get-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

set -e

SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
BASE_VERSION=$(cat ${SCRIPT_DIR}/../../CURRENT_VERSION)
BRANCH=$(git rev-parse --abbrev-ref HEAD)
GIT_HASH=$(git rev-parse HEAD)

if [ "${GITHUB_REF}" != "" ]; then
TAG_VERSION=${GITHUB_REF#refs/tags/}

# this isn't a tag
if [ ${TAG_VERSION} == ${GITHUB_REF} ]; then
echo ${BASE_VERSION}-${GIT_HASH}
else
echo ${BASE_VERSION}
fi
else
if $(git diff --quiet); then
echo ${BASE_VERSION}-${GIT_HASH}
else
echo ${BASE_VERSION}-${GIT_HASH}.localchanges
fi
fi
5 changes: 4 additions & 1 deletion src/ci/set-versions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

set -ex

VERSION=$(cat CURRENT_VERSION)
SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
GET_VERSION=${SCRIPT_DIR}/get-version.sh
VERSION=$(${GET_VERSION})
cd ${SCRIPT_DIR}/../../

SET_VERSIONS="src/pytypes/onefuzztypes/__version__.py src/api-service/__app__/onefuzzlib/__version__.py src/cli/onefuzz/__version__.py"
SET_REQS="src/cli/requirements.txt src/api-service/__app__/requirements.txt"
Expand Down
17 changes: 17 additions & 0 deletions src/ci/unset-versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

set -ex

SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
GET_VERSION=${SCRIPT_DIR}/get-version.sh
VERSION=$(${GET_VERSION})
cd ${SCRIPT_DIR}/../../

SET_VERSIONS="src/pytypes/onefuzztypes/__version__.py src/api-service/__app__/onefuzzlib/__version__.py src/cli/onefuzz/__version__.py"
SET_REQS="src/cli/requirements.txt src/api-service/__app__/requirements.txt"

sed -i 's/__version__ = .*/__version__ = "0.0.0"/' ${SET_VERSIONS}
sed -i "s/onefuzztypes==.*/onefuzztypes==0.0.0/" ${SET_REQS}
41 changes: 32 additions & 9 deletions src/proxy-manager/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
Expand All @@ -6,7 +7,7 @@ use std::process::Command;
fn run_cmd(args: &[&str]) -> Result<String, Box<dyn Error>> {
let cmd = Command::new(args[0]).args(&args[1..]).output()?;
if cmd.status.success() {
Ok(String::from_utf8_lossy(&cmd.stdout).to_string())
Ok(String::from_utf8_lossy(&cmd.stdout).trim().to_string())
} else {
Err(From::from("failed"))
}
Expand All @@ -16,21 +17,43 @@ fn read_file(filename: &str) -> Result<String, Box<dyn Error>> {
let mut file = File::open(filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
contents = contents.trim().to_string();

Ok(contents)
}

fn main() -> Result<(), Box<dyn Error>> {
fn print_version(include_sha: bool) -> Result<(), Box<dyn Error>> {
let mut version = read_file("../../CURRENT_VERSION")?;
let sha = run_cmd(&["git", "rev-parse", "HEAD"])?;
let with_changes = if run_cmd(&["git", "diff", "--quiet"]).is_err() {
"-local_changes"
} else {
""
};
println!("cargo:rustc-env=GIT_VERSION={}{}", sha, with_changes);

let version = read_file("../../CURRENT_VERSION")?;
if include_sha {
version.push('-');
version.push_str(&sha);

// if we're a non-release buil, check to see if git has
bmc-msft marked this conversation as resolved.
Show resolved Hide resolved
// unstaged changes
if run_cmd(&["git", "diff", "--quiet"]).is_err() {
version.push('.');
version.push_str("localchanges");
}
}

println!("cargo:rustc-env=GIT_VERSION={}", sha);
println!("cargo:rustc-env=ONEFUZZ_VERSION={}", version);

Ok(())
}

fn main() -> Result<(), Box<dyn Error>> {
// If we're built off of a tag, we accept CURRENT_VERSION as is. Otherwise
// modify it to indicate local build
if let Ok(val) = env::var("GITHUB_REF") {
if val.starts_with("refs/tags/") {
print_version(false)
} else {
print_version(true)
}
} else {
print_version(true)
}
}