简体   繁体   中英

I want to write a python code in Ruby script, how can that be done?

I have the following code in Python script 1.py that executes read_config.rb ruby script when run.

from Naked.toolshed.shell import execute_rb, muterun_rb
success = execute_rb('read_config.rb')

Now here is a read_config.rb Ruby file

from my-vpc-netapp-only import Variables      # Python line of code to be executed 
require 'erb'
require 'yaml'

region= 'ap-south-1'
k8sver='1.18'
vpccidr= '192.168.0.0/16'
vpcid = Variables.VPC_ID
pubsubnet1a = Variables.PUBSUBNET1A
pubsubnet1b = Variables.PUBSUBNET1B
prisubnet1a = Variables.PRISUBNET1A
prisubnet1b = Variables.PRISUBNET1B

template = ERB.new File.read 'managedcluster.yaml.erb'

File.open('managedcluster.yaml', 'w') do |f|
  f.write template.result(binding)
end

In this Ruby file, I am importing class Variables from another python script. What changes could be made so that scripts get executed without any error?

As I understand, my-vpc.netapp-only is a running app, and thats why you need to automate fetching variable values using an import equivalent. Otherwise you could handpick some variables from my-vpc.netapp-only.py . in that assumption, it is a matter of writing

PUBSUBNET1A
PUBSUBNET1B
PRISUBNET1A
PRISUBNET1B

to a file whenever it changes while running my-vpc.netapp-only.py and you read it in ruby script to make the yaml file.

In this ruby file i am importing class Variables from another python script. What changes could be made to so that scripts get executed without any error?

The line is not syntactically valid Ruby, so the first thing you would need to do is to modify it such that it becomes valid Ruby. Maybe something like this:

import(:Variables, from: :'my-vpc-netapp-only')

Step #2 is to write a Python interpreter in Ruby which provides an import method that can import Python modules as Ruby objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM