-
Notifications
You must be signed in to change notification settings - Fork 79
Implement package upload to our storage backend on Digital Ocean #110
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
thomashoneyman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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" | ||
f-f marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
-} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
f-f marked this conversation as resolved.
Show resolved
Hide resolved
|
||
; | ||
} 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); | ||
} | ||
}); | ||
}); | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.