diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..43e62e2f3 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,30 @@ +repos: + - repo: https://github.com/pycqa/isort + rev: 5.8.0 + hooks: + - id: isort + name: isort (python) + - id: isort + name: isort (cython) + types: [cython] + - id: isort + name: isort (pyi) + types: [pyi] + - repo: https://github.com/psf/black + rev: 20.8b1 + hooks: + - id: black + - repo: https://gitlab.com/pycqa/flake8 + rev: 3.9.0 + hooks: + - id: flake8 + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.3.0 + hooks: + - id: check-added-large-files + - id: check-case-conflict + - id: check-json + - id: check-merge-conflict + - id: detect-private-key + - id: end-of-file-fixer + - id: trailing-whitespace diff --git a/.travis.yml b/.travis.yml index 6ea44a971..1e0d56c0f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ install: # Python - pip freeze - pip install -r monkey/monkey_island/requirements.txt # for unit tests -- pip install black flake8 pytest pytest-cov isort # for next stages +- pip install black==20.8b1 flake8==3.9.0 pytest pytest-cov isort==5.8.0 # for next stages - pip install coverage # for code coverage - pip install -r monkey/infection_monkey/requirements.txt # for unit tests - pip install pipdeptree diff --git a/deployment_scripts/README.md b/deployment_scripts/README.md index 4ee91b5b4..ff767b33b 100644 --- a/deployment_scripts/README.md +++ b/deployment_scripts/README.md @@ -16,7 +16,7 @@ Invoke-WebRequest https://raw.githubusercontent.com/guardicore/monkey/develop/de This will download our deploy script. It's a good idea to read it quickly before executing it! -After downloading that script, execute it in `powershell`. +After downloading that script, execute it in `powershell`. The first argument is an empty directory (script can create one). The second argument is which branch you want to clone - by default, the script will check out the `develop` branch. Some example usages: @@ -63,3 +63,22 @@ After the `deploy_linux.sh` script completes, you can start the monkey island. cd infection_monkey/monkey ./monkey_island/linux/run.sh ``` + +## Pre-commit hooks + +Both the Linux and Windows deployment scrips will install and configure +[pre-commit](https://pre-commit.com/). Pre-commit is a multi-language package +manager for pre-commit hooks. It will run a set of checks when you attempt to +commit. If your commit does not pass all checks, it will be reformatted and/or +you'll be given a list of errors and warnings that need to be fixed before you +can commit. + +Our CI system runs the same checks when pull requests are submitted. This +system may report that the build has failed if the pre-commit hooks have not +been run or all issues have not been resolved. + +### Manually installing pre-commit + +To install and configure pre-commit manually, run `pip install --user +pre-commit`. Next, go to the top level directory of this repository and run +`pre-commit install` Now, pre-commit will automatically run whenever you `git commit`. diff --git a/deployment_scripts/deploy_linux.sh b/deployment_scripts/deploy_linux.sh index 408aa3148..940b763d5 100755 --- a/deployment_scripts/deploy_linux.sh +++ b/deployment_scripts/deploy_linux.sh @@ -23,6 +23,13 @@ log_message() { echo -e "DEPLOYMENT SCRIPT: $1" } +configure_precommit() { + $1 -m pip install --user pre-commit + pushd "$2" + $HOME/.local/bin/pre-commit install + popd +} + if is_root; then log_message "Please don't run this script as root" exit 1 @@ -225,5 +232,7 @@ fi sudo chmod +x "${INFECTION_MONKEY_DIR}/build_linux.sh" +configure_precommit ${python_cmd} ${monkey_home} + log_message "Deployment script finished." exit 0 diff --git a/deployment_scripts/deploy_windows.ps1 b/deployment_scripts/deploy_windows.ps1 index 85a3f0698..28d34904c 100644 --- a/deployment_scripts/deploy_windows.ps1 +++ b/deployment_scripts/deploy_windows.ps1 @@ -9,6 +9,23 @@ param( [Bool] $agents = $true ) + +function Configure-precommit([String] $git_repo_dir) +{ + Write-Output "Installing pre-commit and setting up pre-commit hook" + Push-Location $git_repo_dir + python -m pip install pre-commit + if ($LastExitCode) { + exit + } + pre-commit install + if ($LastExitCode) { + exit + } + Pop-Location + Write-Output "Pre-commit successfully installed" +} + function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName, [String] $branch = "develop") { Write-Output "Downloading to $monkey_home" @@ -119,6 +136,8 @@ function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName, $scoutsuiteRequirements = Join-Path -Path $monkey_home -ChildPath $SCOUTSUITE_DIR | Join-Path -ChildPath "\requirements.txt" & python -m pip install --user -r $scoutsuiteRequirements + Configure-precommit($monkey_home) + $user_python_dir = cmd.exe /c 'py -m site --user-site' $user_python_dir = Join-Path (Split-Path $user_python_dir) -ChildPath "\Scripts" if (!($ENV:Path | Select-String -SimpleMatch $user_python_dir))