Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement package upload to our storage backend on Digital Ocean #110

Merged
merged 3 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
94 changes: 94 additions & 0 deletions ci/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ci/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"license": "MIT",
"dependencies": {
"@octokit/rest": "^18.0.0",
"aws-sdk": "^2.814.0",
"semver": "^7.3.2",
"xhr2": "^0.2.0"
}
Expand Down
57 changes: 57 additions & 0 deletions ci/src/PackageUpload.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module PackageUpload where

import Prelude

import Data.Array as Array
import Effect (Effect)
import Effect.Aff as Aff
import Effect.Class (liftEffect)
import Effect.Class.Console (log)
import Node.FS.Aff as FS
import S3 as S3

type PackageInfo =
{ name :: String
, version :: String
, revision :: Int
}

type Path = String

upload :: PackageInfo -> Path -> Effect Unit
upload { name, version, revision } path = Aff.launchAff_ $ do
-- first check that the file path exists
fileContent <- do
fileExists <- FS.exists path
if fileExists
then FS.readFile path
else Aff.throwError $ Aff.error $ "File doesn't exist: " <> show path
-- connect to the bucket
let bucket = "purescript-registry"
log $ "Connecting to the bucket " <> show bucket
s3 <- liftEffect $ S3.connect "ams3.digitaloceanspaces.com"
-- check that the file for that version and revision is there
let filename
= name <> "/"
<> version
<> (if revision == 0 then "" else "_r" <> show revision)
<> ".tar.gz"
let listParams = { "Bucket": bucket, "Prefix": name <> "/" }
publishedPackages <- map _."Key" <$> S3.listObjects s3 listParams
if Array.elem filename publishedPackages
-- if the release is already there we crash
then Aff.throwError $ Aff.error $ "The package " <> show filename <> " already exists"
-- if it's not, we upload it with public read permission
else do
log $ "Uploading release to the bucket: " <> show filename
let putParams = { "Bucket": bucket, "Key": filename, "Body": fileContent, "ACL": "public-read" }
void $ S3.putObject s3 putParams
log "Done."

{-

main :: Effect Unit
main = do
upload { name: "aff", version: "5.1.2", revision: 0 } "../examples/aff/v5.1.2.json"

-}
48 changes: 48 additions & 0 deletions ci/src/S3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const AWS = require('aws-sdk');

if ("SPACES_KEY" in process.env && "SPACES_SECRET" in process.env) {
;
} else {
console.log('Please set SPACES_KEY and SPACES_SECRET envvars');
process.exit(1);
}

exports.connectImpl = function(endpoint) {
return function() {
const spacesEndpoint = new AWS.Endpoint(endpoint);
const s3 = new AWS.S3({
endpoint: spacesEndpoint,
accessKeyId: process.env.SPACES_KEY,
secretAccessKey: process.env.SPACES_SECRET
});
return s3;
};
};

exports.listObjectsImpl = function(s3, params) {
return function() {
return new Promise(function(resolve, reject) {
s3.listObjectsV2(params, function(err, data) {
if (err) {
reject(err);
} else {
resolve(data['Contents']);
}
});
});
};
};

exports.putObjectImpl = function(s3, params) {
return function() {
return new Promise(function(resolve, reject) {
s3.putObject(params, function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
};
54 changes: 54 additions & 0 deletions ci/src/S3.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module S3 (
connect,
listObjects,
putObject,
S3
) where

import Control.Promise (Promise)
import Control.Promise as Promise
import Data.Function.Uncurried (Fn1, Fn2, runFn1, runFn2)
import Data.JSDate (JSDate)
import Effect (Effect)
import Effect.Aff (Aff)
import Node.Buffer (Buffer)

foreign import data S3 :: Type

foreign import connectImpl :: Fn1 String (Effect S3)
connect :: String -> Effect S3
connect = runFn1 connectImpl

-- Add more params as needed:
-- https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listObjectsV2-property
type ListParams =
{ "Bucket" :: String
, "Prefix" :: String
}

type ListResponse =
{ "Key" :: String
, "LastModified" :: JSDate
, "ETag" :: String
, "Size" :: Int
, "StorageClass" :: String
}

foreign import listObjectsImpl :: Fn2 S3 ListParams (Effect (Promise (Array ListResponse)))
listObjects :: S3 -> ListParams -> Aff (Array ListResponse)
listObjects s3 params = Promise.toAffE (runFn2 listObjectsImpl s3 params)

-- Add more params as needed:
-- https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
type PutParams =
{ "Bucket" :: String
, "Key" :: String
, "Body" :: Buffer -- TODO: the SDK also accepts a string here
, "ACL" :: String -- TODO: this should really be an enum
}

type PutResponse = { "ETag" :: String }

foreign import putObjectImpl :: Fn2 S3 PutParams (Effect (Promise PutResponse))
putObject :: S3 -> PutParams -> Aff PutResponse
putObject s3 params = Promise.toAffE (runFn2 putObjectImpl s3 params)