terraform) how to stop instance after running script(inline)? - python

I create 30 instance to run crawl script, and want to stop aws instances after complete running this command, python3 aws_crawl.py, but I cannot find how to stop current aws instance by command line.
Please see my main.tf code below:
resource "aws_instance" "crawl_worker" {
ami = "${var.ami_id}"
instance_type = "t3a.nano"
security_groups = ["${var.aws_security_group}"]
key_name = "secret"
count = 10
tags = {
Name = "crawl_worker_${count.index}"
}
connection {
type = "ssh"
host = "${self.public_ip}"
user = "ubuntu"
private_key = "${file("~/secret.pem")}"
timeout = "50m"
}
provisioner "remote-exec" {
inline = [
"python3 aws_crawl.py"
]
}
}

Related

Calling a Python script from C# returns an empty output

I am creating a web application using ASP NET that runs on Windows Server 2012 using IIS 10. This application exposes a service at www.domain.com/service/execute which uses a python script executed using Python 3.9.13. Below I report the C# and Python code:
main.py
import pprint
pprint.pprint("Hello World!!")
ServiceController.cs
public class ServiceController : Controller {
public IActionResult Execute() {
var FileName = $#"C:\\path\\to\\python.exe";
var Arguments = $#"C:\\path\\to\\main.py";
var processStartInfo = new ProcessStartInfo() {
FileName = FileName,
Arguments = Arguments,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
using(var process = Process.Start(processStartInfo)) {
var taskError = process.StandardError.ReadToEndAsync();
var taskOutput = process.StandardOutput.ReadToEndAsync();
taskError.Wait();
taskOutput.Wait();
var errors = taskError.Result.Trim();
var output = taskOutput.Result.Trim();
return Ok(
$"FileName={FileName}" +
$"\n" +
$"Arguments={Arguments}" +
$"\n" +
$"errors={errors}" +
$"\n" +
$"output={output}"
);
}
}
}
Calling this service I don't get any errors or exceptions but the output is empty:
FileName=C:/path/to/python.exe
Arguments=C:/path/to/main.py
errors=
output=
If in the C# script I insert as Arguments a totally invented path to a non-existent python script I don't get any error/exception and the output is still empty.
I would expect an output like this:
FileName=C:/path/to/python.exe
Arguments=C:/path/to/main.py
errors=
output=Hello World!!

Why is my Android app crashing every time I run a python script with chaquopy?

I am building an Android app that will allow the user to get a picture either by taking it in real time or uploading it from their saved images. Then, it will go through a machine learning script in python to determine their location. Before I completely connect to the algorithm, I am trying a test program that just returns a double.
from os.path import dirname, join
import csv
import random
filename = join(dirname(__file__), "new.csv")
def testlat():
return 30.0
def testlong():
return 30.0
These returned values are used in a Kotlin file that will then send those values to the Google Maps activity on the app for the location to be plotted.
class MainActivity : AppCompatActivity() {
var lat = 0.0
var long = 0.0
var dynamic = false
private val cameraRequest = 1888
lateinit var imageView: ImageView
lateinit var button: Button
private val pickImage = 100
private var imageUri: Uri? = null
var active = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Accesses the info image button
val clickMe = findViewById<ImageButton>(R.id.imageButton)
// Runs this function when the info icon is pressed by the user
// It will display the text in the variable infoText
clickMe.setOnClickListener {
Toast.makeText(this, infoText, Toast.LENGTH_LONG).show()
}
if (ContextCompat.checkSelfPermission(applicationContext, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
cameraRequest
)
}
imageView = findViewById(R.id.imageView)
val photoButton: Button = findViewById(R.id.button2)
photoButton.setOnClickListener {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, cameraRequest)
dynamic = true
}
/*
The below will move to external photo storage once button2 is clicked
*/
button = findViewById(R.id.button)
button.setOnClickListener {
val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
startActivityForResult(gallery, pickImage)
}
// PYTHON HERE
if (! Python.isStarted()) {
Python.start(AndroidPlatform(this))
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && requestCode == pickImage) {
imageUri = data?.data
imageView.setImageURI(imageUri)
// PYTHON HERE
val py = Python.getInstance()
val pyobj = py.getModule("main")
this.lat = pyobj.callAttr("testlat").toDouble()
this.long = pyobj.callAttr("testlong").toDouble()
/* Open the map after image has been received from user
This will be changed later to instead call the external object recognition/pathfinding
scripts and then pull up the map after those finish running
*/
val mapsIntent = Intent(this, MapsActivity::class.java)
startActivity(mapsIntent)
}
}
}
I set up chaquopy and the gradle is building successfully, but everytime I get to the python part of emulating the app, it crashes. I'm not quite sure why that is; I thought maybe the program was too much for the phone to handle but it is a very basic python script so I doubt that's the issue.
If your app crashes, you can find the stack trace in the Logcat.
In this case, it's probably caused by the line return = 30.0. The correct syntax is return 30.0.

Cross-program invocation with unauthorized signer or writable account

I am trying to create a transaction on python for the program that I wrote, and I am falling into an issue that I can't really pinpoint how to solve it.
program_id = PublicKey("9qm7AEJFHQ8SqJrmfofWK6maWRwKvQwK8uy8w3PVZLQw")
program_id_account_meta = AccountMeta(program_id, False, False)
payer_account_meta = AccountMeta(payer_keypair.public_key, True, False)
vault_account_meta = AccountMeta(PublicKey("G473EkeR5gowVn8CRwTSDop3zPwaNixwp62qi7nyVf4z"), False, False)
accounts = [
program_id_account_meta,
payer_account_meta,
vault_account_meta]
transaction = Transaction()
transaction.add(TransactionInstruction(
accounts,
program_id,
bytes([0])
))
client.send_transaction(transaction, payer_keypair)
When I execute it, I get the error whose image is attached.
I did play with setting the payer_account_meta's writable value to True, and no luck.
I am attaching my solana program's code here, just incase that could be the source of the error, although it seems the error occurs before my program is being even sent the transaction for execution.
entrypoint!(process_instructions);
pub enum Instructions{
CreateAccount {
},
}
impl Instructions{
fn unpackinst(input: &[u8]) -> Result<Self, ProgramError>{
let (&instr, _) = input.split_first().ok_or(ProgramError::InvalidArgument)?;
Ok(match instr{
0 => {
Self::CreateAccount{
}
}
_ => return Err(ProgramError::InvalidInstructionData.into())
})
}
}
pub fn process_instructions(program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8])-> ProgramResult{
let instruction = Instructions::unpackinst(instruction_data)?;
let account_info_iter = &mut accounts.iter();
match instruction {
Instructions::CreateAccount{
} => {
let payer_account_info = next_account_info(account_info_iter)?;
let vault = next_account_info(account_info_iter)?;
let temp_key = Pubkey::from_str("G473EkeR5gowVn8CRwTSDop3zPwaNixwp62qi7nyVf4z").unwrap();
if vault.key != &temp_key && program_id != program_id {
Err(ProgramError::InvalidAccountData)?
}
let price: u64 = (0.5 * (i32::pow(10,9)) as f64) as u64;
invoke(
&system_instruction::transfer(
&payer_account_info.key,
&temp_key,
price,
),
&[
payer_account_info.clone(),
vault.clone()
]
)?;}} Ok(())}
You're attempting to transfer between accounts that aren't declared as writable, so the runtime is correctly saying that there has been privilege escalation during your invocation of system_instruction::transfer.
Try changing your Python AccountMeta declarations to:
payer_account_meta = AccountMeta(payer_keypair.public_key, True, True)
vault_account_meta = AccountMeta(PublicKey("G473EkeR5gowVn8CRwTSDop3zPwaNixwp62qi7nyVf4z"), False, True)
More info at https://docs.solana.com/developing/programming-model/runtime#policy, specifically:
Only the owner may change account data.
And if the account is writable.
Here, the owner is the system program during transfer, so it can only change the amounts if the accounts are writable.
Also, you need to pass in the system program in order to invoke it during your program, and not pass in your own program. So you'll want to do instead:
from solana.system_program import SYS_PROGRAM_ID
program_id_account_meta = AccountMeta(SYS_PROGRAM_ID, False, False)
and add it last in your list of accounts, which becomes
accounts = [
payer_account_meta,
vault_account_meta,
program_id_account_meta,
]

How to fix "can't find '__main__' module in '' error?

I am calling my python file from Windows Service
the code is working fine by itself
and the service works fine too
but when I call the python code from the windows service I get this error
my python code is this
import pyodbc
import pandas as pd
ConnectionString = "Driver={SQL Server};Server=XYZ;Database=ABCD;Trusted_Connection=yes;"
conn = pyodbc.connect(ConnectionString)
df_results = pd.read_sql("EXEC TestService" , conn)
and he is my windows service in c#
Log("In cmd", true);
try
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = PythonPath;
string Script = PythonSuggestedDiagnosesFile;
psi.Arguments = $"\"{Script}\"";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
string Errors = "";
string Results = "";
using (var process = Process.Start(psi))
{
Errors = process.StandardError.ReadToEnd();
Results = process.StandardOutput.ReadToEnd();
}
Log("In cmd : " + "Errors:\n" + Errors + "\n\nResults:\n" + Results);
}
catch (Exception ex)
{
Log("ERROR (cmd) : " + ex.ToString());
}
and the error I get is this
In cmd : Errors:
C:\Users\MyID\AppData\Local\Programs\Python\Python310\python.exe: can't find '__main__' module in ''
Results:
how to fix that?
You should pass proper WorkingDirectory to your ProcessStartInfo. Like this.

Nomad : Unable to Run a python script

I am trying to submit the following job to my nomad server. The job basically uses a payload which is a python file from my localhost.
job "agent-collector-bot" {
datacenters = ["staging"]
type = "batch"
periodic {
cron = "*/10 * * * *"
prohibit_overlap = true
}
group "python-bot" {
count = 1
task "slack-bot" {
driver = "raw_exec"
config {
command = "python"
args = ["local/agent-collector-slackbot.py"]
}
dispatch_payload {
file = "agent-collector-slackbot.py"
}
}
}
}
Now when i see the job status in nomad it says:
snomad status agent-collector-bot/
ID = agent-collector-bot/periodic-1512465000
Name = agent-collector-bot/periodic-1512465000
Submit Date = 12/05/17 14:40:00 IST
Type = batch
Priority = 50
Datacenters = staging
Status = pending
Periodic = false
Parameterized = false
Summary
Task Group Queued Starting Running Failed Complete Lost
python-bot 1 0 0 0 0 0
Placement Failure
Task Group "python-bot":
* Constraint "missing drivers" filtered 5 nodes
I checked my nomad clients (all 5) are having python on it.. can someone help me out?
The driver specified in the output is raw_exec, not python.
You need to enable it in your client config (nomad raw_exec docs)
client {
options = {
"driver.raw_exec.enable" = "1"
}
}

Categories