Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
aws
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
aws
Commits
7f606ae8
Commit
7f606ae8
authored
Aug 18, 2024
by
Ahmad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
inikt
parents
Pipeline
#207
canceled with stages
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
111 additions
and
0 deletions
+111
-0
app.js
app.js
+94
-0
package.json
package.json
+17
-0
No files found.
app.js
0 → 100644
View file @
7f606ae8
const
fs
=
require
(
'
fs
'
);
const
path
=
require
(
'
path
'
);
const
csv
=
require
(
'
csv-parser
'
);
const
AWS
=
require
(
'
aws-sdk
'
);
// Define the path to your Downloads directory
const
downloadsPath
=
path
.
join
(
process
.
env
.
HOME
||
process
.
env
.
USERPROFILE
,
'
Downloads
'
);
// Define the path to your rootkey.csv file
const
rootKeyFilePath
=
path
.
join
(
process
.
env
.
HOME
||
process
.
env
.
USERPROFILE
,
'
rootkey.csv
'
);
// Function to get AWS credentials from rootkey.csv
function
getAWSCredentials
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
credentials
=
{};
fs
.
createReadStream
(
rootKeyFilePath
)
.
pipe
(
csv
())
.
on
(
'
data
'
,
(
row
)
=>
{
credentials
.
accessKeyId
=
row
[
'
Access key ID
'
];
credentials
.
secretAccessKey
=
row
[
'
Secret access key
'
];
})
.
on
(
'
end
'
,
()
=>
{
if
(
credentials
.
accessKeyId
&&
credentials
.
secretAccessKey
)
{
resolve
(
credentials
);
}
else
{
reject
(
new
Error
(
'
Failed to load AWS credentials from rootkey.csv
'
));
}
})
.
on
(
'
error
'
,
(
error
)
=>
reject
(
error
));
});
}
// Function to get all .pem files and extract regions
function
getPemFilesAndRegions
()
{
const
pemFiles
=
fs
.
readdirSync
(
downloadsPath
).
filter
(
file
=>
file
.
endsWith
(
'
.pem
'
));
const
regions
=
new
Set
();
// Using Set to avoid duplicate regions
pemFiles
.
forEach
(
file
=>
{
const
match
=
file
.
match
(
/LightsailDefaultKey-
(
.*
)\.
pem/
);
if
(
match
)
{
regions
.
add
(
match
[
1
]);
// Extract region and add to Set
}
});
return
Array
.
from
(
regions
);
// Convert Set to Array
}
// Function to get all Lightsail instances in a given region
async
function
getLightsailInstances
(
region
,
credentials
)
{
const
lightsail
=
new
AWS
.
Lightsail
({
region
,
accessKeyId
:
credentials
.
accessKeyId
,
secretAccessKey
:
credentials
.
secretAccessKey
});
try
{
const
instances
=
await
lightsail
.
getInstances
({}).
promise
();
return
instances
.
instances
;
}
catch
(
err
)
{
console
.
error
(
`Error fetching instances in region
${
region
}
:`
,
err
.
message
);
return
[];
}
}
// Main function to execute the task
async
function
main
()
{
try
{
const
credentials
=
await
getAWSCredentials
();
const
regions
=
getPemFilesAndRegions
();
if
(
regions
.
length
===
0
)
{
console
.
log
(
'
No regions found from .pem files.
'
);
return
;
}
for
(
const
region
of
regions
)
{
console
.
log
(
`Fetching instances in region:
${
region
}
`
);
const
instances
=
await
getLightsailInstances
(
region
,
credentials
);
if
(
instances
.
length
===
0
)
{
console
.
log
(
`No instances found in region:
${
region
}
`
);
}
else
{
instances
.
forEach
(
instance
=>
{
console
.
log
(
`- Instance Name:
${
instance
.
name
}
, State:
${
instance
.
state
.
name
}
`
);
});
}
}
}
catch
(
error
)
{
console
.
error
(
'
Error:
'
,
error
.
message
);
}
}
main
().
catch
(
console
.
error
);
package.json
0 → 100644
View file @
7f606ae8
{
"name"
:
"aws"
,
"version"
:
"1.0.0"
,
"description"
:
""
,
"main"
:
"index.js"
,
"scripts"
:
{
"test"
:
"echo
\"
Error: no test specified
\"
&& exit 1"
},
"keywords"
:
[],
"author"
:
""
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"
aws-sdk
"
:
"
^2.1677.0
"
,
"
csv-parser
"
:
"
^3.0.0
"
,
"
ssh2
"
:
"
^1.15.0
"
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment